-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
72 additions
and
51 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import path from 'path'; | ||
import fsPromise from 'fs/promises'; | ||
import chokidar from 'chokidar'; | ||
|
||
export function copy({watch, transformFn = v => v}) { | ||
return new Promise(resolve => { | ||
const outDir = 'out'; | ||
|
||
async function copyFile(srcFilename) { | ||
const dstFilename = path.join(outDir, srcFilename); | ||
const dirname = path.dirname(dstFilename); | ||
try { | ||
await fsPromise.stat(dirname); | ||
} catch { | ||
await fsPromise.mkdir(dirname, { recursive: true }); | ||
} | ||
console.log('copy', srcFilename, '->', dstFilename); | ||
const src = await fsPromise.readFile(srcFilename); | ||
const dst = transformFn(src, srcFilename, dstFilename); | ||
await fsPromise.writeFile(dstFilename, dst); | ||
} | ||
|
||
chokidar.watch('.', { | ||
ignored: [ | ||
'.git', | ||
'node_modules', | ||
'build', | ||
'out', | ||
'src', | ||
'dist', | ||
'**/.*', | ||
], | ||
}).on('all', (event, path) => { | ||
switch (event) { | ||
case 'add': | ||
case 'change': | ||
copyFile(path); | ||
break; | ||
case 'ready': | ||
if (!watch) { | ||
resolve(); | ||
} | ||
break; | ||
} | ||
}); | ||
}); | ||
} |
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
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
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