From 0230e952cabaa1a49838976994036dabf8028fdc Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 4 May 2020 13:20:10 +0300 Subject: [PATCH] Automatically build missing dependencies when building packages --- packages/browser-tests/build.js | 35 ++++++++++- packages/iconify/build.js | 33 ++++++++-- packages/react/build.js | 33 ++++++++-- packages/svelte/build.js | 104 ++++++++++++++++++++++++++++++++ packages/svelte/package.json | 3 +- packages/vue/build.js | 33 ++++++++-- 6 files changed, 225 insertions(+), 16 deletions(-) create mode 100644 packages/svelte/build.js diff --git a/packages/browser-tests/build.js b/packages/browser-tests/build.js index 1ae84ca..900dac4 100644 --- a/packages/browser-tests/build.js +++ b/packages/browser-tests/build.js @@ -1,6 +1,9 @@ +const fs = require('fs'); const path = require('path'); const child_process = require('child_process'); +const packagesDir = path.dirname(__dirname); + // List of commands to run const commands = []; @@ -11,7 +14,7 @@ const compile = { lib: true, dist: true, }; -process.argv.slice(2).forEach(cmd => { +process.argv.slice(2).forEach((cmd) => { if (cmd.slice(0, 2) !== '--') { return; } @@ -35,7 +38,7 @@ process.argv.slice(2).forEach(cmd => { case 'only': // disable other modules - Object.keys(compile).forEach(key2 => { + Object.keys(compile).forEach((key2) => { compile[key2] = key2 === key; }); break; @@ -43,6 +46,32 @@ process.argv.slice(2).forEach(cmd => { } }); +// Check if required modules in same monorepo are available +const fileExists = (file) => { + try { + fs.statSync(file); + } catch (e) { + return false; + } + return true; +}; + +if (compile.dist && !fileExists(packagesDir + '/browser-tests/lib/node.js')) { + compile.lib = true; +} + +if ( + compile.lib && + (!fileExists(packagesDir + '/iconify/dist/iconify.js') || + !fileExists(packagesDir + '/iconify/lib/iconify.js')) +) { + compile.iconify = true; +} + +if (compile.iconify && !fileExists(packagesDir + '/core/lib/modules.js')) { + compile.core = true; +} + // Compile core before compiling this package if (compile.core) { commands.push({ @@ -61,7 +90,7 @@ if (compile.iconify || compile.core) { } // Compile other packages -Object.keys(compile).forEach(key => { +Object.keys(compile).forEach((key) => { if (key !== 'core' && key !== 'iconify' && compile[key]) { commands.push({ cmd: 'npm', diff --git a/packages/iconify/build.js b/packages/iconify/build.js index ca6ec0e..d9060b4 100644 --- a/packages/iconify/build.js +++ b/packages/iconify/build.js @@ -1,6 +1,9 @@ +const fs = require('fs'); const path = require('path'); const child_process = require('child_process'); +const packagesDir = path.dirname(__dirname); + // List of commands to run const commands = []; @@ -11,7 +14,7 @@ const compile = { dist: true, api: true, }; -process.argv.slice(2).forEach(cmd => { +process.argv.slice(2).forEach((cmd) => { if (cmd.slice(0, 2) !== '--') { return; } @@ -35,7 +38,7 @@ process.argv.slice(2).forEach(cmd => { case 'only': // disable other modules - Object.keys(compile).forEach(key2 => { + Object.keys(compile).forEach((key2) => { compile[key2] = key2 === key; }); break; @@ -43,17 +46,39 @@ process.argv.slice(2).forEach(cmd => { } }); +// Check if required modules in same monorepo are available +const fileExists = (file) => { + try { + fs.statSync(file); + } catch (e) { + return false; + } + return true; +}; + +if (compile.dist && !fileExists(packagesDir + '/iconify/lib/iconify.js')) { + compile.lib = true; +} + +if (compile.api && !fileExists(packagesDir + '/iconify/lib/iconify.d.ts')) { + compile.lib = true; +} + +if (compile.lib && !fileExists(packagesDir + '/core/lib/modules.js')) { + compile.core = true; +} + // Compile core before compiling this package if (compile.core) { commands.push({ cmd: 'npm', args: ['run', 'build'], - cwd: path.dirname(__dirname) + '/core', + cwd: packagesDir + '/core', }); } // Compile other packages -Object.keys(compile).forEach(key => { +Object.keys(compile).forEach((key) => { if (key !== 'core' && compile[key]) { commands.push({ cmd: 'npm', diff --git a/packages/react/build.js b/packages/react/build.js index ca6ec0e..0bcb2f8 100644 --- a/packages/react/build.js +++ b/packages/react/build.js @@ -1,6 +1,9 @@ +const fs = require('fs'); const path = require('path'); const child_process = require('child_process'); +const packagesDir = path.dirname(__dirname); + // List of commands to run const commands = []; @@ -11,7 +14,7 @@ const compile = { dist: true, api: true, }; -process.argv.slice(2).forEach(cmd => { +process.argv.slice(2).forEach((cmd) => { if (cmd.slice(0, 2) !== '--') { return; } @@ -35,7 +38,7 @@ process.argv.slice(2).forEach(cmd => { case 'only': // disable other modules - Object.keys(compile).forEach(key2 => { + Object.keys(compile).forEach((key2) => { compile[key2] = key2 === key; }); break; @@ -43,17 +46,39 @@ process.argv.slice(2).forEach(cmd => { } }); +// Check if required modules in same monorepo are available +const fileExists = (file) => { + try { + fs.statSync(file); + } catch (e) { + return false; + } + return true; +}; + +if (compile.dist && !fileExists(packagesDir + '/react/lib/icon.js')) { + compile.lib = true; +} + +if (compile.api && !fileExists(packagesDir + '/react/lib/icon.d.ts')) { + compile.lib = true; +} + +if (compile.lib && !fileExists(packagesDir + '/core/lib/modules.js')) { + compile.core = true; +} + // Compile core before compiling this package if (compile.core) { commands.push({ cmd: 'npm', args: ['run', 'build'], - cwd: path.dirname(__dirname) + '/core', + cwd: packagesDir + '/core', }); } // Compile other packages -Object.keys(compile).forEach(key => { +Object.keys(compile).forEach((key) => { if (key !== 'core' && compile[key]) { commands.push({ cmd: 'npm', diff --git a/packages/svelte/build.js b/packages/svelte/build.js new file mode 100644 index 0000000..9e9ee4f --- /dev/null +++ b/packages/svelte/build.js @@ -0,0 +1,104 @@ +const fs = require('fs'); +const path = require('path'); +const child_process = require('child_process'); + +const packagesDir = path.dirname(__dirname); + +// List of commands to run +const commands = []; + +// Parse command line +const compile = { + core: false, + dist: true, +}; +process.argv.slice(2).forEach((cmd) => { + if (cmd.slice(0, 2) !== '--') { + return; + } + const parts = cmd.slice(2).split('-'); + if (parts.length === 2) { + // Parse 2 part commands like --with-lib + const key = parts.pop(); + if (compile[key] === void 0) { + return; + } + switch (parts.shift()) { + case 'with': + // enable module + compile[key] = true; + break; + + case 'without': + // disable module + compile[key] = false; + break; + + case 'only': + // disable other modules + Object.keys(compile).forEach((key2) => { + compile[key2] = key2 === key; + }); + break; + } + } +}); + +// Check if required modules in same monorepo are available +const fileExists = (file) => { + try { + fs.statSync(file); + } catch (e) { + return false; + } + return true; +}; + +if (compile.dist && !fileExists(packagesDir + '/core/lib/modules.js')) { + compile.core = true; +} + +// Compile core before compiling this package +if (compile.core) { + commands.push({ + cmd: 'npm', + args: ['run', 'build'], + cwd: packagesDir + '/core', + }); +} + +// Compile other packages +Object.keys(compile).forEach((key) => { + if (key !== 'core' && compile[key]) { + commands.push({ + cmd: 'npm', + args: ['run', 'build:' + key], + }); + } +}); + +/** + * Run next command + */ +const next = () => { + const item = commands.shift(); + if (item === void 0) { + process.exit(0); + } + + if (item.cwd === void 0) { + item.cwd = __dirname; + } + + const result = child_process.spawnSync(item.cmd, item.args, { + cwd: item.cwd, + stdio: 'inherit', + }); + + if (result.status === 0) { + process.nextTick(next); + } else { + process.exit(result.status); + } +}; +next(); diff --git a/packages/svelte/package.json b/packages/svelte/package.json index e653925..4ece2e5 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -15,7 +15,8 @@ "module": "dist/index.mjs", "main": "dist/index.js", "scripts": { - "build": "rollup -c" + "build": "node build", + "build:dist": "rollup -c rollup.config.js" }, "dependencies": { "@iconify/core": "^1.0.0-beta.0", diff --git a/packages/vue/build.js b/packages/vue/build.js index ca6ec0e..4fc072d 100644 --- a/packages/vue/build.js +++ b/packages/vue/build.js @@ -1,6 +1,9 @@ +const fs = require('fs'); const path = require('path'); const child_process = require('child_process'); +const packagesDir = path.dirname(__dirname); + // List of commands to run const commands = []; @@ -11,7 +14,7 @@ const compile = { dist: true, api: true, }; -process.argv.slice(2).forEach(cmd => { +process.argv.slice(2).forEach((cmd) => { if (cmd.slice(0, 2) !== '--') { return; } @@ -35,7 +38,7 @@ process.argv.slice(2).forEach(cmd => { case 'only': // disable other modules - Object.keys(compile).forEach(key2 => { + Object.keys(compile).forEach((key2) => { compile[key2] = key2 === key; }); break; @@ -43,17 +46,39 @@ process.argv.slice(2).forEach(cmd => { } }); +// Check if required modules in same monorepo are available +const fileExists = (file) => { + try { + fs.statSync(file); + } catch (e) { + return false; + } + return true; +}; + +if (compile.dist && !fileExists(packagesDir + '/vue/lib/IconifyIcon.js')) { + compile.lib = true; +} + +if (compile.api && !fileExists(packagesDir + '/vue/lib/IconifyIcon.d.ts')) { + compile.lib = true; +} + +if (compile.lib && !fileExists(packagesDir + '/core/lib/modules.js')) { + compile.core = true; +} + // Compile core before compiling this package if (compile.core) { commands.push({ cmd: 'npm', args: ['run', 'build'], - cwd: path.dirname(__dirname) + '/core', + cwd: packagesDir + '/core', }); } // Compile other packages -Object.keys(compile).forEach(key => { +Object.keys(compile).forEach((key) => { if (key !== 'core' && compile[key]) { commands.push({ cmd: 'npm',