• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overlookmotel / livepack / 7108276868

06 Dec 2023 12:12AM UTC coverage: 90.504% (+0.03%) from 90.474%
7108276868

push

github

overlookmotel
Rename `cache` CLI option [major]

4656 of 5016 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 1 file covered. (0.0%)

5 existing lines in 1 file now uncovered.

12489 of 13928 relevant lines covered (89.67%)

8694.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/cli.js
1
#!/usr/bin/env node
×
2

×
3
/* eslint-disable import/order, import/newline-after-import */
×
4

×
5
'use strict';
×
6

×
7
// Use internal module cache
×
8
const {useInternalModuleCache, useGlobalModuleCache} = require('./shared/moduleCache.js');
×
9
useInternalModuleCache();
×
10

×
11
// Modules
×
12
const {resolve: pathResolve, join: pathJoin, dirname, parse: pathParse} = require('path'),
×
13
        {readFileSync} = require('fs'),
×
14
        yargs = require('yargs'),
×
15
        findUp = require('find-up'),
×
16
        stripJsonComments = require('strip-json-comments'),
×
17
        {isObject, isArray, isString, isBoolean} = require('is-it-type'),
×
18
        assert = require('simple-invariant');
×
19

×
20
// CLI
×
21

×
22
// Throw unhandled rejections
×
23
process.on('unhandledRejection', (err) => {
×
24
        throw err;
×
25
});
×
26

×
27
// Load config file
×
28
const configPath = findUp.sync(['.livepackrc', 'livepack.config.json', 'livepack.config.js']);
×
29
let config;
×
30
if (configPath) {
×
31
        if (/\.js$/.test(configPath)) {
×
32
                config = require(configPath); // eslint-disable-line global-require, import/no-dynamic-require
×
33
        } else {
×
34
                config = JSON.parse(stripJsonComments(readFileSync(configPath, 'utf8')));
×
35
        }
×
36
} else {
×
37
        config = {};
×
38
}
×
39

×
40
// Parse args
×
41
const {argv} = yargs
×
42
        .config(config)
×
43
        .command(
×
44
                '$0',
×
45
                'livepack <input path(s)..> -o <output dir path> [options]',
×
46
                () => {},
×
47
                (_argv) => {
×
48
                        const {input} = _argv;
×
49
                        if (input === undefined) {
×
50
                                const inputs = _argv._;
×
51
                                assert(inputs.length > 0, 'Must specify input file');
×
52
                                _argv.input = inputs;
×
53
                        }
×
54
                }
×
55
        )
×
56
        .alias('help', 'h')
×
57
        .alias('version', 'v')
×
58
        .option('input', {
×
59
                // Only used in config file
×
60
                alias: 'inputs',
×
61
                description: 'Input file path',
×
62
                type: 'string',
×
63
                hidden: true
×
64
        })
×
65
        .option('output', {
×
66
                alias: 'o',
×
67
                description: 'Output directory path',
×
68
                demandOption: true,
×
69
                type: 'string'
×
70
        })
×
71
        .option('format', {
×
72
                alias: 'f',
×
73
                description: 'Output format',
×
74
                type: 'string',
×
75
                choices: ['esm', 'cjs'],
×
76
                default: 'esm'
×
77
        })
×
78
        .option('ext', {
×
79
                description: 'JS file extension',
×
80
                type: 'string',
×
81
                default: 'js'
×
82
        })
×
83
        .option('map-ext', {
×
84
                description: 'Source map file extension',
×
85
                type: 'string',
×
86
                default: 'map'
×
87
        })
×
88
        .option('minify', {
×
89
                alias: 'm',
×
90
                description: 'Minify output',
×
91
                type: 'boolean',
×
92
                default: false
×
93
        })
×
94
        .option('mangle', {
×
95
                type: 'boolean',
×
96
                description: 'Mangle var names'
×
97
        })
×
98
        .option('comments', {
×
99
                type: 'boolean',
×
100
                description: 'Keep comments in output'
×
101
        })
×
102
        .option('entry-chunk-name', {
×
103
                type: 'string',
×
104
                description: 'Template for entry point chunk names'
×
105
        })
×
106
        .option('split-chunk-name', {
×
107
                type: 'string',
×
108
                description: 'Template for split chunk names'
×
109
        })
×
110
        .option('common-chunk-name', {
×
111
                type: 'string',
×
112
                description: 'Template for common chunk names'
×
113
        })
×
114
        .option('source-maps', {
×
115
                alias: 's',
×
116
                description: 'Create source maps',
×
117
                defaultDescription: 'false',
×
118
                type: 'string',
×
119
                coerce(val) {
×
120
                        if (val === 'inline' || isBoolean(val) || val === undefined) return val;
×
121
                        if (val === '') return true;
×
122
                        throw new Error("--source-maps option should have no value or 'inline'");
×
123
                }
×
124
        })
×
125
        .option('exec', {
×
126
                description: 'Output executable script',
×
127
                type: 'boolean',
×
128
                default: true
×
129
        })
×
130
        .option('esm', {
×
131
                description: 'ES modules source',
×
132
                type: 'boolean',
×
133
                default: false
×
134
        })
×
135
        .option('jsx', {
×
136
                description: 'JSX source',
×
137
                type: 'boolean',
×
138
                default: false
×
139
        })
×
140
        .option('stats', {
×
141
                description: 'Output stats file',
×
142
                defaultDescription: 'false',
×
143
                type: 'string',
×
144
                coerce(val) {
×
145
                        if (val === '') return true;
×
146
                        if (isString(val) || isBoolean(val) || val === undefined) return val;
×
147
                        throw new Error('--stats option should have no value or string for name of stats file');
×
148
                }
×
149
        })
×
NEW
150
        .option('cache', {
×
NEW
151
                description: 'Enable instrumentation cache',
×
152
                type: 'boolean',
×
153
                default: true
×
154
        })
×
155
        .option('debug', {
×
156
                description: 'Output debug info',
×
157
                type: 'boolean',
×
158
                default: false,
×
159
                hidden: true
×
160
        });
×
161

×
162
// The following is all after yargs option parsing, rather than at top of file,
×
163
// to avoid slow response for `livepack --help` or a command with missing/invalid options.
×
164

×
165
// Catalog globals etc
×
166
require('./init/index.js');
×
167

×
168
// Import dependencies required for serialize
×
169
const {writeFile, mkdir} = require('fs/promises'),
×
170
        {serializeEntries} = require('./serialize/index.js'),
×
171
        {DEFAULT_OUTPUT_FILENAME} = require('./shared/constants.js');
×
172

×
173
// Switch back to global module cache
×
174
useGlobalModuleCache();
×
175

×
176
// Register Babel transform to track scopes
×
177
const register = require('./register/index.js');
×
178

×
179
register({
×
180
        esm: argv.esm,
×
181
        jsx: argv.jsx,
×
NEW
182
        cache: argv.cache
×
183
});
×
184

×
185
// Serialize
×
186
(async () => {
×
187
        // Determine output path
×
188
        const outPath = pathResolve(argv.output);
×
189

×
190
        // Conform inputs array to object
×
191
        let inputs = argv.input;
×
192
        if (isString(inputs)) {
×
193
                inputs = {[DEFAULT_OUTPUT_FILENAME]: inputs};
×
194
        } else if (isArray(inputs)) {
×
195
                const inputsObj = {};
×
196
                for (const path of inputs) {
×
197
                        assert(isString(path), '`input` paths must be strings');
×
198
                        const {name} = pathParse(path);
×
199
                        assert(!inputsObj[name], `Multiple input files with same name '${name}'`);
×
200
                        inputsObj[name] = path;
×
201
                }
×
202
                inputs = inputsObj;
×
203
        } else {
×
204
                assert(isObject(inputs), '`input` must be a string, object or array');
×
205
                for (const [name, path] of Object.entries(inputs)) {
×
206
                        assert(isString(name), '`input` keys must be strings');
×
207
                        assert(isString(path), '`input` paths must be strings');
×
208
                }
×
209
        }
×
210

×
211
        // Load sources
×
212
        const entries = {};
×
213
        await Promise.all(
×
214
                Object.entries(inputs).map(async ([name, pathOriginal]) => {
×
215
                        const path = pathResolve(pathOriginal);
×
216

×
217
                        let entry;
×
218
                        try {
×
219
                                entry = require(path); // eslint-disable-line global-require, import/no-dynamic-require
×
220
                        } catch (err) {
×
221
                                if (
×
222
                                        err && err.code === 'MODULE_NOT_FOUND'
×
223
                                        && err.message.split('\n')[0] === `Cannot find module '${path}'`
×
224
                                        && err.requireStack
×
225
                                        && err.requireStack.length === 1
×
226
                                        && err.requireStack[0] === __filename
×
227
                                ) {
×
228
                                        throw new Error(`Cannot load input file '${pathOriginal}'`);
×
229
                                }
×
230
                                throw err;
×
231
                        }
×
232

×
233
                        if (entry && entry.__esModule) entry = entry.default;
×
234

×
235
                        // Await Promise value
×
236
                        entry = await entry;
×
237

×
238
                        entries[name] = entry;
×
239
                })
×
240
        );
×
241

×
242
        // Serialize
×
243
        const files = serializeEntries(entries, {
×
244
                format: argv.format,
×
245
                ext: argv.ext,
×
246
                mapExt: argv.mapExt,
×
247
                exec: argv.exec,
×
248
                minify: argv.minify,
×
249
                mangle: argv.mangle,
×
250
                comments: argv.comments,
×
251
                entryChunkName: argv.entryChunkName,
×
252
                splitChunkName: argv.splitChunkName,
×
253
                commonChunkName: argv.commonChunkName,
×
254
                sourceMaps: argv.sourceMaps || false,
×
255
                stats: argv.stats,
×
256
                outputDir: argv.sourceMaps ? outPath : undefined,
×
257
                debug: argv.debug
×
258
        });
×
259

×
260
        // Output files
×
261
        for (const {filename, content} of files) {
×
262
                const path = pathJoin(outPath, filename);
×
263
                await mkdir(dirname(path), {recursive: true});
×
264
                await writeFile(path, content);
×
265
        }
×
266
})();
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc