Skip to content

Commit

Permalink
fix watcher path generations
Browse files Browse the repository at this point in the history
  • Loading branch information
AidasPa committed Mar 11, 2021
1 parent e9a6bbb commit 6603273
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 53 deletions.
72 changes: 26 additions & 46 deletions cli/actions/watch.js
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}`);
}
});
};
4 changes: 1 addition & 3 deletions cli/stubs/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
95 changes: 91 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6603273

Please sign in to comment.