From faffc76a762319063dbfc49e0423e8236f6f9337 Mon Sep 17 00:00:00 2001 From: Ibby Date: Thu, 9 Feb 2017 08:48:29 -0500 Subject: [PATCH] chore(): nodejs build script --- package.json | 6 +- scripts/build/build.js | 137 ++++++++++++++++++ ...ackage.json.template => core-package.json} | 0 ...kage.json.template => plugin-package.json} | 0 ...gin.json.template => tsconfig-plugin.json} | 0 5 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 scripts/build/build.js rename scripts/build/{core-package.json.template => core-package.json} (100%) rename scripts/build/{plugin-package.json.template => plugin-package.json} (100%) rename scripts/build/{tsconfig-plugin.json.template => tsconfig-plugin.json} (100%) diff --git a/package.json b/package.json index 06110d84d..cb7d46b34 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dist" ], "dependencies": { + "fs-extra-promise": "^0.4.1", "rxjs": "5.0.0-beta.12" }, "devDependencies": { @@ -22,6 +23,7 @@ "dgeni": "^0.4.2", "dgeni-packages": "^0.10.18", "es6-shim": "~0.35.2", + "fs-extra": "^2.0.0", "glob": "^7.1.1", "gulp": "^3.9.1", "gulp-rename": "^1.2.2", @@ -35,9 +37,9 @@ "karma-phantomjs-launcher": "~1.0.2", "lodash": "4.17.4", "minimist": "^1.1.3", - "mkdirp": "^0.5.1", "node-html-encoder": "0.0.2", "q": "1.4.1", + "queue": "^4.2.1", "semver": "^5.3.0", "tsify": "~3.0.0", "tslint": "^3.15.1", @@ -52,7 +54,7 @@ "lint": "gulp lint", "build": "npm run lint && npm run build:core && npm run build:modules", "build:core": "tsc -p scripts/build/tsconfig-core.json", - "build:modules": "sh scripts/build/build.sh", + "build:modules": "node scripts/build/build.js", "shipit": "npm run build && npm run npmpub", "npmpub": "sh scripts/build/publish.sh", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0", diff --git a/scripts/build/build.js b/scripts/build/build.js new file mode 100644 index 000000000..641dde00d --- /dev/null +++ b/scripts/build/build.js @@ -0,0 +1,137 @@ +// Node module dependencies +const fs = require('fs-extra-promise').useFs(require('fs-extra')), + queue = require('queue'), + path = require('path'), + exec = require('child_process').exec; + +// Constants for the build process. Paths and JSON files templates +const ROOT = path.resolve(path.join(__dirname, '../../')), // root ionic-native directory + VERSION = require(path.resolve(ROOT, 'package.json')).version, // current ionic-native version + PLUGINS_PATH = path.resolve(ROOT, 'src/@ionic-native/plugins'), // path to plugins source files + CORE_PACKAGE_JSON = require(path.resolve(__dirname, 'core-package.json')), // core package.json + PLUGIN_PACKAGE_JSON = require(path.resolve(__dirname, 'plugin-package.json')), // plugin package.json template + PLUGIN_TS_CONFIG = require(path.resolve(__dirname, 'tsconfig-plugin.json')), // plugin tsconfig template + BUILD_TMP = path.resolve(ROOT, '.tmp'), // tmp directory path + BUILD_DIST_ROOT = path.resolve(ROOT, 'dist/packages-dist/@ionic-native'), // dist directory root path + BUILD_PLUGINS_DIST = path.resolve(BUILD_DIST_ROOT, 'plugins'), // plugins dist directory path + BUILD_CORE_DIST = path.resolve(BUILD_DIST_ROOT, 'core'); // core dist directory path + + +// Delete dist directory and any temporary files +console.log('Removing old TMP directory'); +fs.removeSync(BUILD_TMP); +fs.removeSync(BUILD_PLUGINS_DIST); + + +// Create tmp/dist directories +console.log('Making new TMP directory'); +fs.mkdirpSync(BUILD_TMP); + + +// Prepare and copy the core module's package.json +console.log('Preparing core module package.json'); +CORE_PACKAGE_JSON.version = VERSION; +fs.writeJsonSync(path.resolve(BUILD_CORE_DIST, 'package.json'), CORE_PACKAGE_JSON); + + +// Fetch a list of the plugins +const PLUGINS = fs.readdirSync(PLUGINS_PATH); + + +// Create a queue to process tasks +const QUEUE = queue({ + concurrency: 10 +}); + + +// Function to process a single plugin +const addPluginToQueue = pluginName => { + + QUEUE.push((callback) => { + + console.log(`Building plugin: ${pluginName}`); + + const PLUGIN_BUILD_DIR = path.resolve(BUILD_TMP, 'plugins', pluginName), + PLUGIN_SRC_PATH = path.resolve(PLUGINS_PATH, pluginName, 'index.ts'); + + let tsConfigPath; + + fs.mkdirpAsync(PLUGIN_BUILD_DIR) // create tmp build dir + .then(() => fs.mkdirpAsync(path.resolve(BUILD_PLUGINS_DIST, pluginName))) // create dist dir + .then(() => { + + // Write tsconfig.json + const tsConfig = JSON.parse(JSON.stringify(PLUGIN_TS_CONFIG)); + tsConfig.files = [PLUGIN_SRC_PATH]; + // tsConfig.compilerOptions.paths['@ionic-native/core'] = [BUILD_CORE_DIST]; + + tsConfigPath = path.resolve(PLUGIN_BUILD_DIR, 'tsconfig.json'); + + return fs.writeJsonAsync(tsConfigPath, tsConfig); + }) + .then(() => fs.readFileAsync(PLUGIN_SRC_PATH, 'utf-8')) // read the plugin definition + .then((pluginFile) => { + + let postinstall, + regexInstall = /install:\s'(.*)',?\s/g.exec(pluginFile), + regexPlugin = /plugin:\s'(.*)',?\s/g.exec(pluginFile); + + if (regexInstall) { + // console.log('install command exists'); + // postinstall = regexInstall[1]; + + } else if (regexPlugin) { + postinstall = `ionic plugin add ${ regexPlugin[1] }`; + } else { + console.log('nothing was found'); + } + + + // clone package.json + const packageJson = JSON.parse(JSON.stringify(PLUGIN_PACKAGE_JSON)); + + packageJson.name = `@ionic-native/${pluginName}`; + packageJson.version = packageJson.dependencies['@ionic-native/core'] = VERSION; + + if (postinstall) { + // add postinstall script + packageJson.scripts = { postinstall }; + } + + return fs.writeJsonAsync(path.resolve(BUILD_PLUGINS_DIST, pluginName, 'package.json'), packageJson); + }) + .then(() => { + + // compile the plugin + exec(`${ROOT}/node_modules/.bin/tsc -p ${tsConfigPath}`, (err, stdout, stderr) => { + + if (err) { + // oops! something went wrong. + callback(`Building ${pluginName} failed.`); + console.log(stdout); + return; + } + + // we're done with this plugin! + callback(); + + }); + + }) + .catch(callback); + + }); // QUEUE.push end + +}; + +PLUGINS.forEach(addPluginToQueue); + +QUEUE.start((err) => { + + if (err) { + console.log('Error building plugins. ', err); + } else { + console.log('Done processing plugins!'); + } + +}); diff --git a/scripts/build/core-package.json.template b/scripts/build/core-package.json similarity index 100% rename from scripts/build/core-package.json.template rename to scripts/build/core-package.json diff --git a/scripts/build/plugin-package.json.template b/scripts/build/plugin-package.json similarity index 100% rename from scripts/build/plugin-package.json.template rename to scripts/build/plugin-package.json diff --git a/scripts/build/tsconfig-plugin.json.template b/scripts/build/tsconfig-plugin.json similarity index 100% rename from scripts/build/tsconfig-plugin.json.template rename to scripts/build/tsconfig-plugin.json