diff --git a/cli/actions/watch.js b/cli/actions/watch.js index 4910bbe..d99f954 100644 --- a/cli/actions/watch.js +++ b/cli/actions/watch.js @@ -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}`); } }); }; diff --git a/cli/stubs/make.js b/cli/stubs/make.js index 868b7a5..faa3e04 100644 --- a/cli/stubs/make.js +++ b/cli/stubs/make.js @@ -20,10 +20,8 @@ module.exports = async (stubName, argArray, filePath) => { .replace('__ROOTPATH__', utils.calculateRootPath(finalPath)); try { - const splitPath = finalPath.split('/'); - splitPath.pop(); + const directory = utils.getPathDirs(finalPath); - const directory = splitPath.join('/'); await utils.createDirsRecursive(directory); await fs.writeFile(`${finalPath}`, computedStub, 'utf-8'); } catch (error) { diff --git a/package-lock.json b/package-lock.json index 423640a..99c704d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -110,6 +110,15 @@ "color-convert": "^1.9.0" } }, + "anymatch": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz", + "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==", + "requires": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + } + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", @@ -159,6 +168,11 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, + "binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==" + }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -168,6 +182,14 @@ "concat-map": "0.0.1" } }, + "braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "requires": { + "fill-range": "^7.0.1" + } + }, "call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", @@ -244,6 +266,21 @@ } } }, + "chokidar": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.1.tgz", + "integrity": "sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==", + "requires": { + "anymatch": "~3.1.1", + "braces": "~3.0.2", + "fsevents": "~2.3.1", + "glob-parent": "~5.1.0", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.5.0" + } + }, "cliui": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", @@ -706,6 +743,14 @@ "debounce": "^1.0.0" } }, + "fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "requires": { + "to-regex-range": "^5.0.1" + } + }, "find-up": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", @@ -736,6 +781,12 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "optional": true + }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -781,7 +832,6 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dev": true, "requires": { "is-glob": "^4.0.1" } @@ -887,6 +937,14 @@ "integrity": "sha512-J0ELF4yHFxHy0cmSxZuheDOz2luOdVvqjwmEcj8H/L1JHeuEDSDbeRP+Dk9kFVk5RTFzbucJ2Kb9F7ixY2QaCg==", "dev": true }, + "is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "requires": { + "binary-extensions": "^2.0.0" + } + }, "is-boolean-object": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-boolean-object/-/is-boolean-object-1.1.0.tgz", @@ -920,8 +978,7 @@ "is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=", - "dev": true + "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=" }, "is-fullwidth-code-point": { "version": "3.0.0", @@ -932,7 +989,6 @@ "version": "4.0.1", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz", "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==", - "dev": true, "requires": { "is-extglob": "^2.1.1" } @@ -943,6 +999,11 @@ "integrity": "sha512-2z6JzQvZRa9A2Y7xC6dQQm4FSTSTNWjKIYYTt4246eMTJmIo0Q+ZyOsU66X8lxK1AbB92dFeglPLrhwpeRKO6w==", "dev": true }, + "is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==" + }, "is-number-object": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-number-object/-/is-number-object-1.0.4.tgz", @@ -1146,6 +1207,11 @@ } } }, + "normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==" + }, "object-inspect": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.9.0.tgz", @@ -1299,6 +1365,11 @@ "pify": "^2.0.0" } }, + "picomatch": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", + "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==" + }, "pify": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", @@ -1361,6 +1432,14 @@ "read-pkg": "^2.0.0" } }, + "readdirp": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz", + "integrity": "sha512-cMhu7c/8rdhkHXWsY+osBhfSy0JikwpHK/5+imo+LpeasTF8ouErHrlYkwT0++njiyuDvc7OFY5T3ukvZ8qmFQ==", + "requires": { + "picomatch": "^2.2.1" + } + }, "regexpp": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.1.0.tgz", @@ -1599,6 +1678,14 @@ "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, + "to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "requires": { + "is-number": "^7.0.0" + } + }, "tsconfig-paths": { "version": "3.9.0", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz", diff --git a/package.json b/package.json index 9ab1346..3dfd14c 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "camel-case": "^4.1.2", + "chokidar": "^3.5.1", "colors": "^1.4.0", "filewatcher": "^3.0.1", "glob": "^7.1.6", diff --git a/utils.js b/utils.js index 711ec57..e3178ad 100644 --- a/utils.js +++ b/utils.js @@ -15,6 +15,16 @@ module.exports = { getLastPathPart(path) { return path.split('/').pop(); }, + getPathDirs(path, shouldShift = false) { + const splitPath = path.split('/'); + splitPath.pop(); + + if (shouldShift) { + splitPath.shift(); + } + + return splitPath.join('/'); + }, calculateRootPath(path) { const matches = path.match(/\//g) || []; const len = matches.length;