Updated packager for test builds

This commit is contained in:
Angelos Chalaris
2018-10-10 22:30:10 +03:00
parent 26361fb93f
commit 0f334e1431
3 changed files with 1517 additions and 21 deletions

View File

@ -9,17 +9,11 @@ const util = require('./util');
const { rollup } = require('rollup');
const babel = require('rollup-plugin-babel');
const minify = require('rollup-plugin-babel-minify');
// Check Travis builds - needs some extra work
if (util.isTravisCI() && util.isNotTravisCronOrAPI()) {
console.log(
`${chalk.green('NOBUILD')} Module build terminated, not a cron job or a custom build!`
);
process.exit(0);
}
// Set variables for paths
const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
const IMPORTS = './imports.js';
const TEST_PACKAGE = './test/_30s.js';
const MODULE_NAME = '_30s';
const DIST = './dist';
// Regex for selecting code blocks
@ -35,11 +29,13 @@ const codeRE = /```\s*js([\s\S]*?)```/;
const snippetExports = `module.exports = {${snippets.map(v => v.replace('.md', '')).join(',')}}`;
let requires = [];
let importData = '';
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH);
const archivedSnippets = fs.readdirSync(SNIPPETS_ARCHIVE_PATH).filter(v => v !== 'README.md');
const testExports = `module.exports = {${[...snippets,...archivedSnippets].map(v => v.replace('.md', '')).join(',')}}`;
// Create `temp` and `dist` folders if they don't already exist.
if (!fs.existsSync(DIST)) fs.mkdirSync(DIST);
// Write `imports.js`
fs.writeFileSync(IMPORTS, '');
fs.writeFileSync(TEST_PACKAGE, '');
snippets.forEach(snippet => {
const snippetData = fs.readFileSync(path.join(SNIPPETS_PATH, snippet), 'utf8');
@ -53,7 +49,24 @@ const codeRE = /```\s*js([\s\S]*?)```/;
});
// Write the data to the imports file
requires = [...new Set(requires.filter(Boolean).map(v => v[0].replace('require(', 'typeof require !== "undefined" && require(')))].join('\n');
fs.writeFileSync(IMPORTS, `${requires}\n\n${importData}\n\n${snippetExports}`)
fs.writeFileSync(IMPORTS, `${requires}\n\n${importData}\n\n${snippetExports}`);
archivedSnippets.forEach(snippet => {
const snippetData = fs.readFileSync(path.join(SNIPPETS_ARCHIVE_PATH, snippet), 'utf8');
let code = snippetData.match(codeRE)[1].replace('\n', '');
importData += code;
});
fs.writeFileSync(TEST_PACKAGE, `${requires}\n\n${importData}\n\n${testExports}`);
// Check Travis builds - Will skip builds on Travis if not CRON/API
if (!util.isTravisCI() && util.isNotTravisCronOrAPI()) {
fs.unlink(IMPORTS);
console.log(
`${chalk.green('NOBUILD')} Module build terminated, not a cron job or a custom build!`
);
console.timeEnd('Packager');
process.exit(0);
}
// Write to the proper files and start the `rollup` script
const es5 = babel({
@ -61,33 +74,30 @@ const codeRE = /```\s*js([\s\S]*?)```/;
});
const min = minify({ comments: false });
const bundle = await rollup({ input: IMPORTS });
const bundleES5 = await rollup({ input: IMPORTS, plugins: [es5] });
const bundleES5Min = await rollup({
input: IMPORTS,
plugins: [es5, min]
});
// UMD ES2018
await bundle.write({
file: `${DIST}/${MODULE_NAME}.js`,
name: MODULE_NAME,
format: 'umd'
});
// ESM ES2018
await bundle.write({
file: `${DIST}/${MODULE_NAME}.esm.js`,
name: MODULE_NAME,
format: 'es'
});
// UMD ES5
const bundleES5 = await rollup({ input: IMPORTS, plugins: [es5] });
await bundleES5.write({
file: `${DIST}/${MODULE_NAME}.es5.js`,
name: MODULE_NAME,
format: 'umd'
});
// UMD ES5 min
const bundleES5Min = await rollup({
input: IMPORTS,
plugins: [es5, min]
});
await bundleES5Min.write({
file: `${DIST}/${MODULE_NAME}.es5.min.js`,
name: MODULE_NAME,

View File

@ -105,8 +105,9 @@ const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
// Checks if current environment is Travis CI
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
const isNotTravisCronOrAPI = () =>
process.env['TRAVIS_EVENT_TYPE'] !== 'cron' && process.env['TRAVIS_EVENT_TYPE'] !== 'api';
const isTravisCronOrAPI = () =>
process.env['TRAVIS_EVENT_TYPE'] === 'cron' && process.env['TRAVIS_EVENT_TYPE'] === 'api';
const isNotTravisCronOrAPI = () => !isTravisCronOrAPI();
// Creates a hash for a value using the SHA-256 algorithm.
const hashData = val =>
crypto
@ -167,6 +168,7 @@ module.exports = {
shuffle,
getCodeBlocks,
getTextualContent,
isTravisCronOrAPI,
isNotTravisCronOrAPI,
prepTaggedData
};

1484
test/_30s.js Normal file

File diff suppressed because it is too large Load Diff