Skip to content

Commit

Permalink
build(psm-tool): pasteImports reconstructs single-file script
Browse files Browse the repository at this point in the history
 - ignore Google Apps Scripts config
  • Loading branch information
dckc committed Sep 7, 2022
1 parent f6a70f6 commit ab4c975
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/psm-tool/app/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.clasp.json
10 changes: 8 additions & 2 deletions packages/psm-tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"./package.json": "./package.json"
},
"scripts": {
"build": "mkdir -p app/build; sed 's/^export .*//' src/psm-lib.js >app/build/psm-lib.js",
"build": "yarn build:script && yarn build:app",
"build:script": "mkdir -p dist; node scripts/pasteImports.js dist/psm-tool src/psm-lib.js scripts/psm-tool.js",
"build:app": "mkdir -p app/build; sed 's/^export .*//' src/psm-lib.js >app/build/psm-lib.js",
"clasp:push": "yarn build && cd app; clasp push",
"test": "ava",
"lint-fix": "yarn lint:eslint --fix",
Expand All @@ -45,13 +47,17 @@
"*.ts",
"LICENSE*",
"app",
"dist",
"src"
],
"publishConfig": {
"access": "public"
},
"eslintConfig": {
"ignorePatterns": "build/*"
"ignorePatterns": [
"dist/*",
"build/*"
]
},
"ava": {
"files": [
Expand Down
33 changes: 33 additions & 0 deletions packages/psm-tool/scripts/pasteImports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check

const dieTrying = () => {
throw Error();
};

/**
*
* @param {string[]} argv
* @param {object} io
* @param {typeof import('fs').promises.readFile} io.readFile
* @param {typeof import('fs').promises.writeFile} io.writeFile
*/
const main = async (argv, { readFile, writeFile }) => {
const dest = argv.shift() || dieTrying();
const mainFn = argv.pop() || dieTrying();
const parts = [];
for await (const fn of argv) {
const txt = await readFile(fn, 'utf-8');
parts.push(txt.replace(/^export /gm, ''));
}
const txt = await readFile(mainFn, 'utf-8');
const script = txt.replace(/import {[^}]*} from [^;]+;/m, parts.join('\n'));

await writeFile(dest, script);
console.info('built', dest, 'from', mainFn, 'with', argv);
};

(async () => {
// eslint-disable-next-line no-undef
const { argv } = process;
import('fs').then(fs => main(argv.slice(2), fs.promises));
})().catch(err => console.error(err));

0 comments on commit ab4c975

Please sign in to comment.