scripts: convert to ESM and deduplicate #290

Merged
norm merged 2 commits from scripts-esm into main 2022-12-16 17:15:26 +00:00
8 changed files with 93 additions and 82 deletions

View file

@ -18,7 +18,7 @@
"migrateandstart": "yarn migrate && yarn start",
"gulp": "gulp build",
"watch": "yarn dev",
"dev": "node ./scripts/dev.js",
"dev": "node ./scripts/dev.mjs",
"lint": "yarn workspaces foreach run lint",
"cy:open": "cypress open --browser --e2e --config-file=cypress.config.ts",
"cy:run": "cypress run",
@ -26,8 +26,8 @@
"mocha": "yarn workspace backend run mocha",
"test": "yarn mocha",
"format": "gulp format",
"clean": "node ./scripts/clean.js",
"clean-all": "node ./scripts/clean-all.js",
"clean": "node ./scripts/clean.mjs",
"clean-all": "node ./scripts/clean-all.mjs",
"cleanall": "yarn clean-all"
},
"resolutions": {

View file

@ -1,17 +0,0 @@
const fs = require('fs');
fs.rmSync(__dirname + '/../packages/backend/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/backend/tsconfig.tsbuildinfo', { force: true });
fs.rmSync(__dirname + '/../packages/backend/node_modules', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/client/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/client/node_modules', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/foundkey-js/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/foundkey-js/node_modules', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/sw/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/sw/node_modules', { recursive: true, force: true });
fs.rmSync(__dirname + '/../built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../node_modules', { recursive: true, force: true });

14
scripts/clean-all.mjs Normal file
View file

@ -0,0 +1,14 @@
import { files, clean } from './clean.mjs';
const allFiles = [
...files,
'/../packages/backend/node_modules',
'/../packages/client/node_modules',
'/../packages/foundkey-js/node_modules',
'/../packages/sw/node_modules',
'/../node_modules',
];
for (const file of allFiles) {
clean(file);
}

View file

@ -1,8 +0,0 @@
const fs = require('fs');
fs.rmSync(__dirname + '/../packages/backend/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/backend/tsconfig.tsbuildinfo', { force: true });
fs.rmSync(__dirname + '/../packages/client/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/foundkey-js/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../packages/sw/built', { recursive: true, force: true });
fs.rmSync(__dirname + '/../built', { recursive: true, force: true });

19
scripts/clean.mjs Normal file
View file

@ -0,0 +1,19 @@
import { rmSync } from 'fs';
import { __dirname } from './common.mjs';
export const files = [
'/../packages/backend/built',
'/../packages/backend/tsconfig.tsbuildinfo',
'/../packages/client/built',
'/../packages/foundkey-js/built',
'/../packages/sw/built',
'/../built',
];
export function clean(filename) {
rmSync(__dirname + filename, { recursive: true, force: true });
}
for (const file of files) {
clean(file);
}

5
scripts/common.mjs Normal file
View file

@ -0,0 +1,5 @@
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
export const __filename = fileURLToPath(import.meta.url);
export const __dirname = dirname(__filename);

View file

@ -1,54 +0,0 @@
const execa = require('execa');
(async () => {
await execa('npm', ['run', 'clean'], {
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
await execa('npm', ['run', 'build'], {
cwd: __dirname + '/../packages/foundkey-js',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npx', ['gulp', 'watch'], {
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/backend',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/client',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/sw',
stdout: process.stdout,
stderr: process.stderr,
});
const start = async () => {
try {
await execa('npm', ['run', 'start'], {
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
} catch (e) {
await new Promise(resolve => setTimeout(resolve, 3000));
start();
}
};
start();
})();

52
scripts/dev.mjs Normal file
View file

@ -0,0 +1,52 @@
import execa from 'execa';
import { __dirname } from './common.mjs';
await execa('npm', ['run', 'clean'], {
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
await execa('npm', ['run', 'build'], {
cwd: __dirname + '/../packages/foundkey-js',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npx', ['gulp', 'watch'], {
Johann150 marked this conversation as resolved
Review

Why are some of these awaited and some not?

Why are some of these `await`ed and some not?
Review

These were the case in the original dev.js script, not sure if we should just run all of these processes asynchronously.

These were the case in the original `dev.js` script, not sure if we should just run all of these processes asynchronously.
Review

I think the first two do need the await since it doesn't make sense to clean asynchronously and pretty much every package depends on foundkey-js being built.

I think the first two do need the `await` since it doesn't make sense to clean asynchronously and pretty much every package depends on `foundkey-js` being built.
Review

The first two produce a result (cleaning out stuff, building things), the non-awaited commands are watchers, where you probably will have a hard time waiting for a result being returned.

The first two produce a result (cleaning out stuff, building things), the non-awaited commands are watchers, where you probably will have a hard time waiting for a result being returned.
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/backend',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/client',
stdout: process.stdout,
stderr: process.stderr,
});
execa('npm', ['run', 'watch'], {
cwd: __dirname + '/../packages/sw',
stdout: process.stdout,
stderr: process.stderr,
});
const start = async () => {
try {
await execa('npm', ['run', 'start'], {
cwd: __dirname + '/../',
stdout: process.stdout,
stderr: process.stderr,
});
} catch (e) {
setTimeout(start, 3000);
}
norm marked this conversation as resolved
Review

Can't this be simplified to just setTimeout(start, 3000); ?

Can't this be simplified to just `setTimeout(start, 3000);` ?
Review

Hmm I'm not so sure any more but I think the change was fine because start isn't awaited on anyway...

Hmm I'm not so sure any more but I think the change was fine because `start` isn't awaited on anyway...
};
start();