From adf6be10a9bb636bcc0dc949c083841ed890bc93 Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 12 Sep 2019 19:53:39 +0300 Subject: [PATCH] test + silent compile --- compile/index.js | 2 ++ package.json | 2 +- src/lib/compile.js | 51 +++++++++++------------------------------- src/lib/index.js | 8 +------ test/mask/default.js | 21 ++++++++++++++++- test/result/compile.md | 9 ++++++++ types/compile.xml | 3 +++ types/externs.js | 2 +- 8 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 test/result/compile.md diff --git a/compile/index.js b/compile/index.js index 89bfbdb..09aa5ab 100644 --- a/compile/index.js +++ b/compile/index.js @@ -6,6 +6,7 @@ const { _Compile } = require('./depack') * @param {string} options.src The entry file to bundle. Currently only single files are supported. * @param {boolean} [options.noStrict=false] Removes `use strict` from the output. Default `false`. * @param {boolean} [options.verbose=false] Print all arguments to the compiler. Default `false`. + * @param {boolean} [options.silent=false] If output is not given, don't print to `stdout`. By default, the output will be printed. Default `false`. * @param {boolean} [options.library=false] Whether to create a library. Default `false`. * @param {!_depack.RunConfig} [runOptions] General options for running of the compiler. * @param {string} [runOptions.output] The path where the output will be saved. Prints to `stdout` if not passed. @@ -56,6 +57,7 @@ module.exports.Compile = Compile * @prop {string} src The entry file to bundle. Currently only single files are supported. * @prop {boolean} [noStrict=false] Removes `use strict` from the output. Default `false`. * @prop {boolean} [verbose=false] Print all arguments to the compiler. Default `false`. + * @prop {boolean} [silent=false] If output is not given, don't print to `stdout`. By default, the output will be printed. Default `false`. * @prop {boolean} [library=false] Whether to create a library. Default `false`. */ diff --git a/package.json b/package.json index 1de4e02..c9324f2 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "compile/index.js", "module": "src/index.js", "scripts": { - "t": "zoroaster -a -t 10000", + "t": "zoroaster -a -t 20000", "test": "yarn t test/spec test/mask", "spec": "yarn t test/spec", "mask": "yarn t test/mask", diff --git a/src/lib/compile.js b/src/lib/compile.js index fdc2191..477478a 100644 --- a/src/lib/compile.js +++ b/src/lib/compile.js @@ -14,19 +14,11 @@ import run from './run' /** * Compile a Node.JS file into a single executable. * @param {!_depack.CompileConfig} options Options for the Node.JS package compiler. - * @param {string} options.src The entry file to bundle. Currently only single files are supported. - * @param {boolean} [options.noStrict=false] Removes `use strict` from the output. Default `false`. - * @param {boolean} [options.verbose=false] Print all arguments to the compiler. Default `false`. - * @param {boolean} [options.library=false] Whether to create a library. Default `false`. - * @param {!_depack.RunConfig} runOptions General options for running of the compiler. - * @param {string} [runOptions.output] The path where the output will be saved. Prints to `stdout` if not passed. - * @param {string} [runOptions.debug] The name of the file where to save sources after each pass. Useful when there's a bug in GCC. - * @param {string} [runOptions.compilerVersion] Used in the display message. - * @param {boolean} [runOptions.noSourceMap=false] Disables source maps. Default `false`. - * @param {!Array} compilerArgs The compiler args got with `getOptions` and/or manually extended. + * @param {!_depack.RunConfig} [runOptions] General options for running of the compiler. + * @param {!Array} [compilerArgs] The compiler args got with `getOptions` and/or manually extended. */ const Compile = async (options, runOptions = {}, compilerArgs = []) => { - const { src, noStrict, verbose, library } = options + const { src, noStrict, verbose, library, silent } = options const { output } = runOptions if (!src) throw new Error('Source is not given.') // allow to pass internals in --externs arg, e.g., @@ -113,12 +105,14 @@ const Compile = async (options, runOptions = {}, compilerArgs = []) => { const stdout = await run(Args, runOptions, library) if (!output) { - const o = prepareOutput(stdout, wrapper, noStrict) - console.log(o.trim()) - } else { - await removeStrict(output, wrapper, noStrict) - await makePromise(chmod, [output, '755']) + const o = prepareOutput(stdout, wrapper, noStrict).trim() + if (!silent) console.log(o) + return o } + + await removeStrict(output, wrapper, noStrict) + await makePromise(chmod, [output, '755']) + return stdout } const printCommand = (args, externs, sorted) => { @@ -227,30 +221,11 @@ export default Compile * @typedef {import('static-analysis').Detection} _staticAnalysis.Detection */ -/* documentary types/compile.xml */ /** * @suppress {nonStandardJsDocs} - * @typedef {_depack.CompileConfig} CompileConfig Options for the Node.JS package compiler. + * @typedef {import('../..').CompileConfig} _depack.CompileConfig */ /** * @suppress {nonStandardJsDocs} - * @typedef {Object} _depack.CompileConfig Options for the Node.JS package compiler. - * @prop {string} src The entry file to bundle. Currently only single files are supported. - * @prop {boolean} [noStrict=false] Removes `use strict` from the output. Default `false`. - * @prop {boolean} [verbose=false] Print all arguments to the compiler. Default `false`. - * @prop {boolean} [library=false] Whether to create a library. Default `false`. - */ - -/* documentary types/index.xml */ -/** - * @suppress {nonStandardJsDocs} - * @typedef {_depack.RunConfig} RunConfig General options for running of the compiler. - */ -/** - * @suppress {nonStandardJsDocs} - * @typedef {Object} _depack.RunConfig General options for running of the compiler. - * @prop {string} [output] The path where the output will be saved. Prints to `stdout` if not passed. - * @prop {string} [debug] The name of the file where to save sources after each pass. Useful when there's a bug in GCC. - * @prop {string} [compilerVersion] Used in the display message. - * @prop {boolean} [noSourceMap=false] Disables source maps. Default `false`. - */ + * @typedef {import('../..').RunConfig} _depack.RunConfig + */ \ No newline at end of file diff --git a/src/lib/index.js b/src/lib/index.js index b24da4f..d6c5407 100644 --- a/src/lib/index.js +++ b/src/lib/index.js @@ -98,12 +98,11 @@ export const checkIfLib = modName => /^[./]/.test(modName) /** * Gets the wrapper to for the output to enable requiring Node.js modules. * @param {!Array} internals The list of internal modules used in the program. - * @param {boolean} [library=false] Whether to create a library. * @example * const fs = require('fs'); * const _module = require('module'); */ -export const getWrapper = (internals, library = false) => { +export const getWrapper = (internals) => { if (!internals.length) return const wrapper = internals .map(i => { @@ -112,11 +111,6 @@ export const getWrapper = (internals, library = false) => { return `const ${m} = r` + `equire('${i}');` // prevent }) .join('\n') + '%output%' - if (library) { - return `'use strict'; -let DEPACK_EXPORT; -${wrapper}` - } return `#!/usr/bin/env node 'use strict'; ${wrapper}` diff --git a/test/mask/default.js b/test/mask/default.js index 8d8437c..b775ed0 100644 --- a/test/mask/default.js +++ b/test/mask/default.js @@ -1,7 +1,7 @@ import makeTestSuite from '@zoroaster/mask' import TempContext from 'temp-context' // import Context from '../context' -import { Bundle, getOptions } from '../../src' +import { Bundle, Compile, getOptions } from '../../src' export const bundle = makeTestSuite('test/result/bundle', { context: TempContext, @@ -17,4 +17,23 @@ export const bundle = makeTestSuite('test/result/bundle', { }, {}, options) return res }, +}) + +export const compile = makeTestSuite('test/result/compile', { + context: TempContext, + /** + * @param {TempContext} t + */ + async getResults({ write }) { + const options = getOptions({ + advanced: true, + languageOut: 2019, + }) + const src = await write('src.js', this.input) + const res = await Compile({ + silent: true, + src, + }, {}, options) + return res + }, }) \ No newline at end of file diff --git a/test/result/compile.md b/test/result/compile.md new file mode 100644 index 0000000..d7da467 --- /dev/null +++ b/test/result/compile.md @@ -0,0 +1,9 @@ +## compiles a js file +import { createReadStream } from 'fs' +createReadStream('test').pipe(process.stdout) + +/* expected */ +#!/usr/bin/env node +'use strict'; +const fs = require('fs'); const {createReadStream:a}=fs;a("test").pipe(process.stdout); +/**/ \ No newline at end of file diff --git a/types/compile.xml b/types/compile.xml index 0a805b4..20e7f53 100644 --- a/types/compile.xml +++ b/types/compile.xml @@ -9,6 +9,9 @@ Print all arguments to the compiler. + + If output is not given, don't print to `stdout`. By default, the output will be printed. + Whether to create a library. diff --git a/types/externs.js b/types/externs.js index 0e6b2e6..c2136a6 100644 --- a/types/externs.js +++ b/types/externs.js @@ -22,7 +22,7 @@ _depack.BundleConfig /* typal types/compile.xml externs */ /** * Options for the Node.JS package compiler. - * @typedef {{ src: string, noStrict: (boolean|undefined), verbose: (boolean|undefined), library: (boolean|undefined) }} + * @typedef {{ src: string, noStrict: (boolean|undefined), verbose: (boolean|undefined), silent: (boolean|undefined), library: (boolean|undefined) }} */ _depack.CompileConfig