-
Notifications
You must be signed in to change notification settings - Fork 3
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
5 changed files
with
129 additions
and
53 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,67 +1,47 @@ | ||
const filewatcher = require('filewatcher'); | ||
const colors = require('colors'); | ||
const fs = require('fs').promises; | ||
const chokidar = require('chokidar'); | ||
const cli = require('../cli'); | ||
const compiler = require('../../compiler'); | ||
const typescript = require('../../compiler/typescript'); | ||
const utils = require('../../utils'); | ||
|
||
const removeFileFromPathString = (path) => { | ||
const split = path.split('/'); | ||
split.pop(); | ||
return split.join('/'); | ||
}; | ||
|
||
module.exports = () => { | ||
// eslint-disable-next-line global-require | ||
const glob = require('glob'); | ||
// glob('src/**/*.ts', (err, matches) => { | ||
// matches.forEach((match) => typescriptWatcher.add(match)); | ||
// }); | ||
|
||
const watcher = filewatcher(); | ||
const typescriptWatcher = filewatcher(); | ||
// glob('src/**/*.uts', (err, matches) => { | ||
// matches.forEach((match) => watcher.add(match)); | ||
// }); | ||
|
||
glob('**/*.ts', (err, matches) => { | ||
matches.forEach((match) => typescriptWatcher.add(match)); | ||
}); | ||
chokidar.watch('src').on('all', async (event, path) => { | ||
const file = path.replace(/\\/g, '/'); | ||
|
||
glob('**/*.uts', (err, matches) => { | ||
matches.forEach((match) => watcher.add(match)); | ||
}); | ||
|
||
console.log(`[${colors.bold.magenta('UTS')}] Watching ${colors.cyan.italic('.uts files')}...`); | ||
console.log(`[${colors.bold.cyan('TS')}] Watching ${colors.cyan.italic('.ts files')}...`); | ||
|
||
typescriptWatcher.on('change', async (file, stat) => { | ||
if (stat) { | ||
console.log(`[${colors.bold.cyan('TS')}] Compiling ${colors.cyan.italic(file)}...`); | ||
if (file.split('.')[1] === 'uts' || file.split('.')[1] === 'ts') { | ||
const fileType = file.split('.')[1]; | ||
const code = await utils.readCode(file); | ||
const compiled = typescript(code); | ||
|
||
try { | ||
await fs.mkdir(`out/${removeFileFromPathString(file)}`, { recursive: true }); | ||
await utils.createDirsRecursive(`out/${utils.getPathDirs(file, true)}`); | ||
} catch (error) { | ||
// silent | ||
// silently fail | ||
} | ||
|
||
utils.writeCode(`out/${file}`, compiled); | ||
} else { | ||
fs.unlink(`out/${file}`); | ||
} | ||
}); | ||
|
||
watcher.on('change', async (file, stat) => { | ||
if (stat) { | ||
console.log(`[${colors.bold.magenta('UTS')}] Compiling ${colors.cyan.italic(file)}...`); | ||
const code = await utils.readCode(file); | ||
const compiled = compiler(code); | ||
let compiled; | ||
|
||
try { | ||
await fs.mkdir(`out/${removeFileFromPathString(file)}`, { recursive: true }); | ||
} catch (error) { | ||
// silent | ||
if (fileType === 'uts') { | ||
compiled = compiler(code); | ||
} else if (fileType === 'ts') { | ||
compiled = typescript(code); | ||
} | ||
|
||
utils.writeCode(`out/${file}`, compiled); | ||
await utils.writeCode( | ||
`out/${utils.getPathDirs(file, true)}/${utils.getLastPathPart( | ||
file.split('.')[0], | ||
)}.js`, | ||
compiled, | ||
); | ||
} else { | ||
fs.unlink(`out/${file}`); | ||
// cli.error(`Should delete ${file}`); | ||
} | ||
}); | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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