Skip to content

Commit

Permalink
Move build steps to js scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
danyeaw committed Jan 28, 2025
1 parent aef53b4 commit c4bc347
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@
"url": "https://github.com/jupyter/nbclassic.git"
},
"scripts": {
"bower:cp-umd": "cp nbclassic/static/components/marked/lib/marked.umd.js nbclassic/static/components/marked/lib/marked.js",
"bower:copy-umd": "copy nbclassic/static/components/marked/lib/marked.umd.js nbclassic/static/components/marked/lib/marked.js",
"bower": "bower install && (npm run bower:cp-umd || npm run bower:copy-umd)",
"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:translations": "node tools/build-translations.js",
"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",
Expand Down
56 changes: 56 additions & 0 deletions tools/build-translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { spawn } = require('child_process');
const path = require('path');

const translations = [
{ locale: 'fr_FR' },
{ locale: 'ja_JP' },
{ locale: 'nl' },
{ locale: 'ru_RU' },
{ locale: 'zh_CN' }
];

async function buildTranslation(locale) {
const baseDir = path.join('nbclassic', 'i18n', locale, 'LC_MESSAGES');
const poFile = path.join(baseDir, 'nbjs.po');
const jsonFile = path.join(baseDir, 'nbjs.json');

return new Promise((resolve, reject) => {
const process = spawn('po2json', [
'-p', // pretty print
'-F', // force overwrite
'-f', 'jed1.x', // format
'-d', 'nbjs', // domain
poFile,
jsonFile
]);

process.stdout.on('data', (data) => {
console.log(`${locale}: ${data}`);
});

process.stderr.on('data', (data) => {
console.error(`${locale} error: ${data}`);
});

process.on('close', (code) => {
if (code === 0) {
console.log(`✓ Built translation for ${locale}`);
resolve();
} else {
reject(new Error(`Failed to build translation for ${locale}`));
}
});
});
}

async function buildAllTranslations() {
try {
await Promise.all(translations.map(({ locale }) => buildTranslation(locale)));
console.log('All translations built successfully!');
} catch (error) {
console.error('Error building translations:', error);
process.exit(1);
}
}

buildAllTranslations();
28 changes: 28 additions & 0 deletions tools/copy-marked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');

const sourceFile = path.join('nbclassic', 'static', 'components', 'marked', 'lib', 'marked.umd.js');
const destFile = path.join('nbclassic', 'static', 'components', 'marked', 'lib', 'marked.js');

// Check file existence
const sourceExists = fs.existsSync(sourceFile);
const destExists = fs.existsSync(destFile);

if (!sourceExists && !destExists) {
// Both files missing - critical error
console.error(`Error: ${destFile} is required but cannot be created (${sourceFile} not found)`);
process.exit(1);
} else if (!sourceExists) {
// Source missing but dest exists - skip copy
console.log(`Copy skipped: ${sourceFile} not found (using existing ${destFile})`);
process.exit(0);
}

// Source exists, attempt copy
try {
fs.copyFileSync(sourceFile, destFile);
console.log(`Successfully copied ${sourceFile} to ${destFile}`);
} catch (err) {
console.error(`Error copying file: ${err.message}`);
process.exit(1);
}

0 comments on commit c4bc347

Please sign in to comment.