refactor(build): lint build scripts

This commit is contained in:
Daniel
2018-03-23 10:57:54 +01:00
parent d7829e4012
commit c15b78bab2
12 changed files with 201 additions and 121 deletions
+26 -12
View File
@@ -1,18 +1,30 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as webpack from 'webpack';
import * as uglifyJsPlugin from 'uglifyjs-webpack-plugin';
import * as unminifiedPlugin from 'unminified-webpack-plugin';
import { cleanEmittedData, EMIT_PATH, InjectableClassEntry } from '../build/transformers/extract-injectables';
import * as webpack from 'webpack';
import { ROOT } from '../build/helpers';
import {
cleanEmittedData,
EMIT_PATH,
InjectableClassEntry
} from '../build/transformers/extract-injectables';
import { Logger } from '../logger';
const DIST = path.resolve(ROOT, 'dist');
const INDEX_PATH = path.resolve(DIST, 'index.js');
const INJECTABLE_CLASSES = fs.readJSONSync(EMIT_PATH).map((item: InjectableClassEntry) => {
item.file = './' + item.file.split(/[\/\\]+/).slice(-4, -1).join('/');
return item;
});
const INJECTABLE_CLASSES = fs
.readJSONSync(EMIT_PATH)
.map((item: InjectableClassEntry) => {
item.file =
'./' +
item.file
.split(/[\/\\]+/)
.slice(-4, -1)
.join('/');
return item;
});
const webpackConfig: webpack.Configuration = {
mode: 'production',
@@ -31,14 +43,16 @@ const webpackConfig: webpack.Configuration = {
}
},
module: {
rules: [{
test: /\.js$/,
use: path.resolve(ROOT, 'scripts/build/remove-tslib-helpers.js')
}]
rules: [
{
test: /\.js$/,
use: path.resolve(ROOT, 'scripts/build/remove-tslib-helpers.js')
}
]
},
plugins: [
new webpack.ProvidePlugin({
'__extends': ['tslib', '__extends']
__extends: ['tslib', '__extends']
}),
new webpack.optimize.OccurrenceOrderPlugin(true),
new webpack.DefinePlugin({
@@ -52,7 +66,7 @@ const webpackConfig: webpack.Configuration = {
};
function getPluginImport(entry: InjectableClassEntry) {
return `import { ${ entry.className } } from '${ entry.file }';`;
return `import { ${entry.className} } from '${entry.file}';`;
}
function createIndexFile() {
+27 -10
View File
@@ -1,27 +1,44 @@
import { generateDeclarations, transpile } from '../build/transpile';
import { EMIT_PATH } from '../build/transformers/extract-injectables';
import { PLUGIN_PATHS } from '../build/helpers';
import * as fs from 'fs-extra';
import * as path from 'path';
import { PLUGIN_PATHS } from '../build/helpers';
import { EMIT_PATH } from '../build/transformers/extract-injectables';
import { generateDeclarations, transpile } from '../build/transpile';
generateDeclarations();
transpile();
const outDirs = PLUGIN_PATHS.map(p => p.replace('src', 'dist').replace(/[\\/]index.ts/, ''));
const outDirs = PLUGIN_PATHS.map(p =>
p.replace('src', 'dist').replace(/[\\/]index.ts/, '')
);
const injectableClasses = fs.readJSONSync(EMIT_PATH);
outDirs.forEach(dir => {
const classes = injectableClasses.filter(entry => entry.dirName === dir.split(/[\\/]+/).pop());
const classes = injectableClasses.filter(
entry => entry.dirName === dir.split(/[\\/]+/).pop()
);
let jsFile: string = fs.readFileSync(path.join(dir, 'index.js'), 'utf-8'),
dtsFile: string = fs.readFileSync(path.join(dir, 'index.d.ts'), 'utf-8');
classes.forEach(entry => {
dtsFile = dtsFile.replace(`class ${ entry.className } `, 'class ' + entry.className + 'Original ');
dtsFile += `\nexport declare const ${ entry.className }: ${ entry.className }Original;`;
jsFile = jsFile.replace(new RegExp(`([\\s\\(])${ entry.className }([\\s\\.;\\(,])`, 'g'), '$1' + entry.className + 'Original$2');
jsFile = jsFile.replace(`export { ${ entry.className }Original }`, `var ${ entry.className } = new ${ entry.className }Original();\nexport { ${ entry.className } }`);
dtsFile = dtsFile.replace(
`class ${entry.className} `,
'class ' + entry.className + 'Original '
);
dtsFile += `\nexport declare const ${entry.className}: ${
entry.className
}Original;`;
jsFile = jsFile.replace(
new RegExp(`([\\s\\(])${entry.className}([\\s\\.;\\(,])`, 'g'),
'$1' + entry.className + 'Original$2'
);
jsFile = jsFile.replace(
`export { ${entry.className}Original }`,
`var ${entry.className} = new ${entry.className}Original();\nexport { ${
entry.className
} }`
);
});
fs.writeFileSync(path.join(dir, 'index.js'), jsFile, 'utf-8');
+7 -1
View File
@@ -1,4 +1,10 @@
import { generateDeclarationFiles, transpileNgx, transpileNgxCore, modifyMetadata, cleanupNgx } from '../build/ngx';
import {
cleanupNgx,
generateDeclarationFiles,
modifyMetadata,
transpileNgx,
transpileNgxCore
} from '../build/ngx';
transpileNgxCore();
transpileNgx();
+29 -20
View File
@@ -1,25 +1,27 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { merge } from 'lodash';
import { exec } from 'child_process';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { cpus } from 'os';
import * as Queue from 'async-promise-queue';
import { exec } from 'child_process';
import * as fs from 'fs-extra';
import { merge } from 'lodash';
import { cpus } from 'os';
import * as path from 'path';
import { PLUGIN_PATHS, ROOT } from '../build/helpers';
import { Logger } from '../logger';
// tslint:disable-next-line:no-var-requires
const MAIN_PACKAGE_JSON = require('../../package.json');
const VERSION = MAIN_PACKAGE_JSON.version;
const FLAGS = '--access public --tag beta';
const PACKAGE_JSON_BASE = {
'description': 'Ionic Native - Native plugins for ionic apps',
'module': 'index.js',
'typings': 'index.d.ts',
'author': 'ionic',
'license': 'MIT',
'repository': {
'type': 'git',
'url': 'https://github.com/ionic-team/ionic-native.git'
description: 'Ionic Native - Native plugins for ionic apps',
module: 'index.js',
typings: 'index.d.ts',
author: 'ionic',
license: 'MIT',
repository: {
type: 'git',
url: 'https://github.com/ionic-team/ionic-native.git'
}
};
@@ -32,7 +34,7 @@ const CORE_VERSION = '^5.0.0';
const PLUGIN_PEER_DEPENDENCIES = {
'@ionic-native/core': VERSION, // TODO change this in production
'rxjs': RXJS_VEERSION
rxjs: RXJS_VEERSION
};
function getPackageJsonContent(name, peerDependencies = {}) {
@@ -52,33 +54,40 @@ function writePackageJson(data: any, dir: string) {
function prepare() {
// write @ionic-native/core package.json
writePackageJson(
getPackageJsonContent('core', { 'rxjs': RXJS_VEERSION }),
getPackageJsonContent('core', { rxjs: RXJS_VEERSION }),
path.resolve(DIST, 'core')
);
// write plugin package.json files
PLUGIN_PATHS.forEach((pluginPath: string) => {
const pluginName = pluginPath.split(/[\/\\]+/).slice(-2)[0];
const packageJsonContents = getPackageJsonContent(pluginName, PLUGIN_PEER_DEPENDENCIES);
const packageJsonContents = getPackageJsonContent(
pluginName,
PLUGIN_PEER_DEPENDENCIES
);
const dir = path.resolve(DIST, 'plugins', pluginName);
writePackageJson(packageJsonContents, dir);
});
}
async function publish(ignoreErrors: boolean = false) {
async function publish(ignoreErrors = false) {
Logger.profile('Publishing');
// upload 1 package per CPU thread at a time
const worker = Queue.async.asyncify((pkg: any) => {
new Promise<any>((resolve, reject) => {
exec(`npm publish ${ pkg } ${ FLAGS }`, (err, stdout) => {
exec(`npm publish ${pkg} ${FLAGS}`, (err, stdout) => {
if (stdout) {
Logger.log(stdout.trim());
resolve(stdout);
}
if (err) {
if (!ignoreErrors) {
if (err.message.includes('You cannot publish over the previously published version')) {
if (
err.message.includes(
'You cannot publish over the previously published version'
)
) {
Logger.verbose('Ignoring duplicate version error.');
return resolve();
}