-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
82 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,62 @@ | ||
|
||
import chokidar from 'chokidar'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const debug = console.log; //() => {}; | ||
const removeLeadingSlash = s => s.replace(/^\//, ''); | ||
const removeLeadingSlash = (s) => s.replace(/^\//, ''); | ||
|
||
/** | ||
* Recursively copies files and watches for changes. | ||
* | ||
* | ||
* Example: | ||
* | ||
* copyAndWatch([ | ||
* {src: "src\/**\/*.js", srcPrefix: "src", dst: "out"}, // would copy src/bar/moo.js -> out/bar/moo.js | ||
* {src: "index.html", dst: "out"}, // copies index.html -> out/index.html | ||
* ]); | ||
* | ||
* | ||
* @param {*} paths [{src: glob, srcPrefix: string, dst: string }] | ||
* @param {*} options { watch: true/false } // watch: false = just copy and exit. | ||
*/ | ||
export function copyAndWatch(paths, { watch } = { watch: true }) { | ||
for (const {src, srcPrefix, dst} of paths) { | ||
for (const { src, srcPrefix, dst } of paths) { | ||
const watcher = chokidar.watch(src, { | ||
ignored: /(^|[\/\\])\../, // ignore dot files | ||
persistent: watch, | ||
}); | ||
|
||
const makeDstPath = (path, dst) => `${dst}/${removeLeadingSlash(path.startsWith(srcPrefix) ? path.substring(srcPrefix.length) : path)}`; | ||
const makeDstPath = (path, dst) => | ||
`${dst}/${removeLeadingSlash( | ||
path.startsWith(srcPrefix) ? path.substring(srcPrefix.length) : path | ||
)}`; | ||
|
||
watcher | ||
.on('addDir', (srcPath, stats) => { | ||
.on('addDir', (srcPath) => { | ||
const dstPath = makeDstPath(srcPath, dst); | ||
debug('addDir:', srcPath, dstPath); | ||
fs.mkdirSync(dstPath, { recursive: true }); | ||
}) | ||
.on('add', (srcPath, stats) => { | ||
.on('add', (srcPath) => { | ||
const dstPath = makeDstPath(srcPath, dst); | ||
const dir = path.dirname(dstPath); | ||
fs.mkdirSync(dir, { recursive: true }); | ||
debug('add:', srcPath, dstPath); | ||
fs.copyFileSync(srcPath, dstPath); | ||
}) | ||
.on('change', (srcPath, stats) => { | ||
.on('change', (srcPath) => { | ||
const dstPath = makeDstPath(srcPath, dst); | ||
debug('change:', srcPath, dstPath); | ||
fs.copyFileSync(srcPath, dstPath); | ||
}) | ||
.on('unlink', (srcPath, stats) => { | ||
.on('unlink', (srcPath) => { | ||
const dstPath = makeDstPath(srcPath, dst); | ||
debug('unlink:', srcPath, dstPath); | ||
fs.unlinkSync(dstPath); | ||
}) | ||
.on('ready', () => { | ||
if (!watch) { | ||
watcher.close(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import {spawn} from 'child_process'; | ||
import {mkdirSync} from 'fs'; | ||
import { spawn } from 'child_process'; | ||
import { mkdirSync } from 'fs'; | ||
|
||
mkdirSync('out', { recursive: true }); | ||
|
||
spawn('node', [ | ||
'build/tools/copy.js', | ||
], { | ||
spawn('node', ['build/tools/copy.js'], { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
||
spawn('./node_modules/.bin/rollup', [ | ||
"-c" | ||
], { | ||
spawn('./node_modules/.bin/rollup', ['-c'], { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import {copyAndWatch} from '../lib/copyAndWatch.js'; | ||
import { copyAndWatch } from '../lib/copyAndWatch.js'; | ||
|
||
const watch = !!process.argv[2]; | ||
|
||
copyAndWatch([ | ||
{src: "public/**/*", srcPrefix: "public", dst: "out"}, | ||
{src: "meshes/**/*", dst: "out"}, | ||
{src: "sample/**/*", dst: "out"}, | ||
{src: "shaders/**/*", dst: "out"}, | ||
{src: "index.html", dst: "out"}, | ||
], { watch }); | ||
copyAndWatch( | ||
[ | ||
{ src: 'public/**/*', srcPrefix: 'public', dst: 'out' }, | ||
{ src: 'meshes/**/*', dst: 'out' }, | ||
{ src: 'sample/**/*', dst: 'out' }, | ||
{ src: 'shaders/**/*', dst: 'out' }, | ||
{ src: 'index.html', dst: 'out' }, | ||
], | ||
{ watch } | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,19 @@ | ||
import {spawn} from 'child_process'; | ||
import {mkdirSync} from 'fs'; | ||
import { spawn } from 'child_process'; | ||
import { mkdirSync } from 'fs'; | ||
|
||
mkdirSync('out', { recursive: true }); | ||
|
||
spawn('npm', [ | ||
'run', | ||
'watch', | ||
], { | ||
spawn('npm', ['run', 'watch'], { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
||
spawn('node', [ | ||
'build/tools/copy.js', | ||
'1', | ||
], { | ||
spawn('node', ['build/tools/copy.js', '1'], { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
||
spawn('./node_modules/.bin/servez', [ | ||
"out" | ||
], { | ||
spawn('./node_modules/.bin/servez', ['out'], { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters