Skip to content

Commit

Permalink
test + silent compile
Browse files Browse the repository at this point in the history
  • Loading branch information
zavr-1 committed Sep 12, 2019
1 parent e332f9c commit adf6be1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 48 deletions.
2 changes: 2 additions & 0 deletions compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`.
*/

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 13 additions & 38 deletions src/lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} compilerArgs The compiler args got with `getOptions` and/or manually extended.
* @param {!_depack.RunConfig} [runOptions] General options for running of the compiler.
* @param {!Array<string>} [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.,
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
*/
8 changes: 1 addition & 7 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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 => {
Expand All @@ -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}`
Expand Down
21 changes: 20 additions & 1 deletion test/mask/default.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
},
})
9 changes: 9 additions & 0 deletions test/result/compile.md
Original file line number Diff line number Diff line change
@@ -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);
/**/
3 changes: 3 additions & 0 deletions types/compile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<prop boolean name="verbose" default="false">
Print all arguments to the compiler.
</prop>
<prop boolean name="silent" default="false">
If output is not given, don't print to `stdout`. By default, the output will be printed.
</prop>
<prop boolean name="library" default="false">
Whether to create a library.
</prop>
Expand Down
2 changes: 1 addition & 1 deletion types/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit adf6be1

Please sign in to comment.