Skip to content

Commit

Permalink
Offload npm scripts to a build.js
Browse files Browse the repository at this point in the history
  • Loading branch information
danyeaw committed Feb 7, 2025
1 parent 107f1f6 commit 0355df2
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 25 deletions.
36 changes: 11 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,17 @@
"type": "git",
"url": "https://github.com/jupyter/nbclassic.git"
},
"scripts": {
"bower:copy-marked": "node tools/copy-marked.js",
"bower": "bower install && npm run bower:copy-marked",
"build:webpack": "webpack --mode production",
"build:notebook": "node tools/build-main.js notebook",
"build:tree": "node tools/build-main.js tree",
"build:edit": "node tools/build-main.js edit",
"build:terminal": "node tools/build-main.js terminal",
"build:auth": "node tools/build-main.js auth",
"build:fr-translation": "po2json -p -F -f jed1.x -d nbjs nbclassic/i18n/fr_FR/LC_MESSAGES/nbjs.po nbclassic/i18n/fr_FR/LC_MESSAGES/nbjs.json",
"build:ja-translation": "po2json -p -F -f jed1.x -d nbjs nbclassic/i18n/ja_JP/LC_MESSAGES/nbjs.po nbclassic/i18n/ja_JP/LC_MESSAGES/nbjs.json",
"build:nl-translation": "po2json -p -F -f jed1.x -d nbjs nbclassic/i18n/nl/LC_MESSAGES/nbjs.po nbclassic/i18n/nl/LC_MESSAGES/nbjs.json",
"build:ru-translation": "po2json -p -F -f jed1.x -d nbjs nbclassic/i18n/ru_RU/LC_MESSAGES/nbjs.po nbclassic/i18n/ru_RU/LC_MESSAGES/nbjs.json",
"build:zh-translation": "po2json -p -F -f jed1.x -d nbjs nbclassic/i18n/zh_CN/LC_MESSAGES/nbjs.po nbclassic/i18n/zh_CN/LC_MESSAGES/nbjs.json",
"build:translations": "npm run build:fr-translation && npm run build:ja-translation && npm run build:nl-translation && npm run build:ru-translation && npm run build:zh-translation",
"build:js": "npm run build:notebook && npm run build:tree && npm run build:edit && npm run build:terminal && npm run build:auth && npm run build:translations",
"build:css-ipython": "lessc --source-map --include-path='nbclassic/static/style' nbclassic/static/style/ipython.less nbclassic/static/style/ipython.min.css",
"build:css-style": "lessc --source-map --include-path='nbclassic/static/style' nbclassic/static/style/style.less nbclassic/static/style/style.min.css",
"build:css": "npm run build:css-ipython && npm run build:css-style",
"build": "npm run bower && npm run build:webpack && npm run build:js && npm run build:css",
"build:watch": "npm run watch",
"watch:css": "onchange 'nbclassic/static/**/*.less' -- npm run build:css",
"watch:js": "onchange 'nbclassic/static/**/!(*.min).js' 'bower.json' -- npm run build:js",
"watch": "npm-run-all --parallel watch:*"
},
"scripts": {
"bower:copy-marked": "node tools/copy-marked.js",
"bower": "bower install && npm run bower:copy-marked",
"build": "npm run bower && node tools/build.js",
"build:clean": "node tools/build.js clean",
"build:webpack": "node tools/build.js webpack",
"build:notebook": "node tools/build.js notebook",
"build:css": "node tools/build.js ipythonCss && node tools/build.js styleCss",
"build:translations": "node tools/build.js translations",
"watch": "npm-run-all --parallel watch:*"
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
Expand Down
119 changes: 119 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const { spawn } = require('child_process');
const fs = require('fs').promises;

const tasks = {
webpack: {
output: 'dist/main.js',
command: ['webpack', '--mode', 'production'],
},
notebook: {
output: 'nbclassic/static/notebook/js/main.min.js',
command: ['node', 'tools/build-main.js', 'notebook'],
},
ipythonCss: {
output: 'nbclassic/static/style/ipython.min.css',
command: ['lessc', '--source-map', '--include-path=nbclassic/static/style',
'nbclassic/static/style/ipython.less', 'nbclassic/static/style/ipython.min.css']
},
styleCss: {
output: 'nbclassic/static/style/style.min.css',
command: ['lessc', '--source-map', '--include-path=nbclassic/static/style',
'nbclassic/static/style/style.less', 'nbclassic/static/style/style.min.css']
},
translations: {
outputs: ['fr_FR', 'ja_JP', 'nl', 'ru_RU', 'zh_CN'].map(lang => {
const langPath = lang.includes('_') ? lang : lang;
return `nbclassic/i18n/${langPath}/LC_MESSAGES/nbjs.json`;
}),
buildFn: async () => {
const languages = ['fr_FR', 'ja_JP', 'nl', 'ru_RU', 'zh_CN'];
for (const lang of languages) {
const langPath = lang.includes('_') ? lang : lang;
const input = `nbclassic/i18n/${langPath}/LC_MESSAGES/nbjs.po`;
const output = `nbclassic/i18n/${langPath}/LC_MESSAGES/nbjs.json`;

console.log(`Building translation for ${lang}...`);
const proc = spawn('po2json', [
'-p', '-F', '-f', 'jed1.x', '-d', 'nbjs',
input, output
], { stdio: 'inherit' });

await new Promise((resolve, reject) => {
proc.on('close', code => {
if (code === 0) resolve();
else reject(new Error(`Translation failed for ${lang} with code ${code}`));
});
});
}
}
}
};

async function runTask(taskName) {
const task = tasks[taskName];
if (!task) {
throw new Error(`Unknown task: ${taskName}`);
}

console.log(`Building ${taskName}...`);
if (task.buildFn) {
await task.buildFn();
} else {
const proc = spawn(task.command[0], task.command.slice(1), { stdio: 'inherit' });
await new Promise((resolve, reject) => {
proc.on('close', code => {
if (code === 0) resolve();
else reject(new Error(`Command failed with code ${code}`));
});
});
}
console.log(`Finished ${taskName}`);
}

async function clean() {
console.log('Cleaning build outputs...');
for (const task of Object.values(tasks)) {
const outputs = task.outputs || [task.output];
for (const output of outputs) {
try {
await fs.unlink(output);
console.log(`Removed ${output}`);
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(`Error removing ${output}:`, err);
}
}
}
}
}

async function runAll() {
for (const taskName of Object.keys(tasks)) {
if (taskName !== 'bower') {
try {
await runTask(taskName);
} catch (err) {
console.error(`Error in task ${taskName}:`, err);
process.exit(1);
}
}
}
}

const command = process.argv[2];
if (command === 'clean') {
clean().catch(err => {
console.error(err);
process.exit(1);
});
} else if (command) {
runTask(command).catch(err => {
console.error(err);
process.exit(1);
});
} else {
runAll().catch(err => {
console.error(err);
process.exit(1);
});
}

0 comments on commit 0355df2

Please sign in to comment.