check-side-effects
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -122,2 +122,4 @@ "use strict"; | ||
output_1 = (_k.sent()).output; | ||
// Delete the temporary input file. | ||
fs_1.unlinkSync(tmpInputFilename); | ||
// Return the chunk code. | ||
@@ -124,0 +126,0 @@ return [2 /*return*/, output_1 |
@@ -57,9 +57,9 @@ #!/usr/bin/env node | ||
var checker_1 = require("./checker"); | ||
function main(opts) { | ||
function main(rawOpts) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var parsedArgs, testFile, testJson, failedExpectations, _i, _a, test, checkSideEffectsOptions, result, modules, checkSideEffectsOptions; | ||
var options, testPath, testJson, failedExpectations, _i, _a, test, esModules, esModulesDescription, checkSideEffectsOptions, result, expectedOutputPath, expectedOutput, failedExpectationsDescription, esModules, checkSideEffectsOptions; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
parsedArgs = minimist_1.default(opts.args, { | ||
options = minimist_1.default(rawOpts.args, { | ||
boolean: [ | ||
@@ -73,2 +73,3 @@ 'help', | ||
'warnings', | ||
'update', | ||
], | ||
@@ -91,2 +92,3 @@ string: ['cwd', 'output', 'test'], | ||
'warnings': false, | ||
'update': false, | ||
'cwd': process.cwd(), | ||
@@ -96,12 +98,13 @@ }, | ||
}); | ||
if (parsedArgs.help) { | ||
if (options.help) { | ||
showHelp(); | ||
return [2 /*return*/]; | ||
} | ||
if (!parsedArgs.test) return [3 /*break*/, 5]; | ||
testFile = path_1.resolve(parsedArgs.cwd, parsedArgs.test); | ||
if (!fs_1.existsSync(testFile)) { | ||
throw "Could not find the test file: " + testFile + "."; | ||
if (!options.test) return [3 /*break*/, 5]; | ||
testPath = path_1.resolve(options.cwd, options.test); | ||
if (!fs_1.existsSync(testPath)) { | ||
throw "Could not find the test file: " + testPath + "."; | ||
} | ||
testJson = JSON.parse(fs_1.readFileSync(testFile, 'utf-8')); | ||
console.log("Loading tests from " + testPath + "\n"); | ||
testJson = JSON.parse(fs_1.readFileSync(testPath, 'utf-8')); | ||
failedExpectations = []; | ||
@@ -113,9 +116,26 @@ _i = 0, _a = testJson.tests; | ||
test = _a[_i]; | ||
checkSideEffectsOptions = __assign({ esModules: test.esModules, cwd: parsedArgs.cwd }, test.options); | ||
esModules = Array.isArray(test.esModules) ? test.esModules : [test.esModules]; | ||
esModulesDescription = esModules.join(' '); | ||
// These tests can take a while. Inform the user of progress. | ||
console.log("Testing " + esModulesDescription); | ||
checkSideEffectsOptions = __assign({ esModules: esModules, cwd: options.cwd }, test.options); | ||
return [4 /*yield*/, checker_1.checkSideEffects(checkSideEffectsOptions)]; | ||
case 2: | ||
result = _b.sent(); | ||
if (result != test.expected) { | ||
failedExpectations.push(test.esModules.join(' ')); | ||
expectedOutputPath = path_1.resolve(options.cwd, test.expectedOutput); | ||
expectedOutput = void 0; | ||
if (fs_1.existsSync(testPath)) { | ||
expectedOutput = fs_1.readFileSync(expectedOutputPath, 'utf-8'); | ||
} | ||
else { | ||
// Don't error out if the file isn't out, because they can be updated afterwards. | ||
expectedOutput = ''; | ||
} | ||
// Check against the expectation. | ||
if (result != expectedOutput) { | ||
failedExpectations.push(esModulesDescription); | ||
if (options.update) { | ||
fs_1.writeFileSync(expectedOutputPath, result, 'utf-8'); | ||
} | ||
} | ||
_b.label = 3; | ||
@@ -126,6 +146,13 @@ case 3: | ||
case 4: | ||
// Print a newline before the results. | ||
console.log(''); | ||
if (failedExpectations.length > 0) { | ||
throw "Tests failed for modules:\n" + | ||
("" + failedExpectations.join('\n')) + | ||
"\n\nRun 'check-side-effects path-to-modules' individually to check results.\n"; | ||
failedExpectationsDescription = failedExpectations.map(function (s) { return " " + s; }).join('\n'); | ||
if (options.update) { | ||
console.log("Tests updated for modules:\n" + failedExpectationsDescription + "\n"); | ||
} | ||
else { | ||
throw "Tests failed for modules:\n" + failedExpectationsDescription + "\n" + | ||
"\nRun 'check-side-effects --test --update' to update the expectations.\n"; | ||
} | ||
} | ||
@@ -137,13 +164,16 @@ else { | ||
case 5: | ||
modules = isNodeBinary(parsedArgs._[0]) ? parsedArgs._.slice(2) : parsedArgs._.slice(1); | ||
esModules = isNodeBinary(options._[0]) ? options._.slice(2) : options._.slice(1); | ||
if (esModules.length == 0) { | ||
throw "You must provide at least one ES module."; | ||
} | ||
checkSideEffectsOptions = { | ||
esModules: modules, | ||
cwd: parsedArgs.cwd, | ||
output: parsedArgs.output, | ||
pureGetters: parsedArgs.pureGetters, | ||
resolveExternals: parsedArgs.resolveExternals, | ||
printDependencies: parsedArgs.printDependencies, | ||
useBuildOptimizer: parsedArgs.useBuildOptimizer, | ||
useMinifier: parsedArgs.useMinifier, | ||
warnings: parsedArgs.warnings, | ||
esModules: esModules, | ||
cwd: options.cwd, | ||
output: options.output, | ||
pureGetters: options.pureGetters, | ||
resolveExternals: options.resolveExternals, | ||
printDependencies: options.printDependencies, | ||
useBuildOptimizer: options.useBuildOptimizer, | ||
useMinifier: options.useMinifier, | ||
warnings: options.warnings, | ||
}; | ||
@@ -163,3 +193,3 @@ // Run it. | ||
function showHelp() { | ||
var helpText = "\ncheck-side-effects [ES modules to check] [--option-name]\n\nChecks side effects from importing given ES modules.\n\nOptions:\n --help Show this message.\n --cwd Override working directory to run the process in.\n --output Output the bundle to this path. Useful to trace the sourcemaps.\n --pure-getters Assume there are no side effects from getters. [Default: true]\n --resolve-externals Resolve external dependencies. [Default: false]\n --print-dependencies Print all the module dependencies. [Default: false]\n --use-build-optimizer Run Build Optimizer over all modules. [Default: true]\n --use-minifier\t Run minifier over the final bundle to remove comments. [Default: true]\n --warnings Show all warnings. [Default: false]\n --test Read a series of tests from a JSON file. [Default: false]\n\nExample:\n check-side-effects ./path/to/library/module.js\n"; | ||
var helpText = "\ncheck-side-effects [ES modules to check] [--option-name]\n\nChecks side effects from importing given ES modules.\n\nOptions:\n --help Show this message.\n --cwd Override working directory to run the process in.\n --output Output the bundle to this path. Useful to trace the sourcemaps.\n --pure-getters Assume there are no side effects from getters. [Default: true]\n --resolve-externals Resolve external dependencies. [Default: false]\n --print-dependencies Print all the module dependencies. [Default: false]\n --use-build-optimizer Run Build Optimizer over all modules. [Default: true]\n --use-minifier\t Run minifier over the final bundle to remove comments. [Default: true]\n --warnings Show all warnings. [Default: false]\n --test Read a series of tests from a JSON file.\n --update Update the test results. [Default: false]\n\nExample:\n check-side-effects ./path/to/library/module.js\n"; | ||
console.log(helpText); | ||
@@ -166,0 +196,0 @@ } |
{ | ||
"name": "check-side-effects", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Check if a ES module has side effects.", | ||
@@ -14,3 +14,3 @@ "repository": "https://github.com/filipesilva/check-side-effects", | ||
"check-side-effects": "npm run build && node dist/cli.js", | ||
"release": "npm version -m \"release: %\"" | ||
"release": "npm run build && npm version -m \"release: %\"" | ||
}, | ||
@@ -17,0 +17,0 @@ "keywords": [], |
@@ -89,8 +89,5 @@ # Check side effects | ||
{ | ||
"esModules": [ | ||
"./path/to/library/module.js", | ||
"./path/to/another-library/module.js" | ||
], | ||
"esModules": "./path/to/library/module.js", | ||
"options": {}, | ||
"expected": "" | ||
"expectedOutput": "./path/to/expected-output.js" | ||
} | ||
@@ -101,4 +98,8 @@ ] | ||
`options` accept the same options as the CLI, expect `help`, but in [Camel Case](https://en.wikipedia.org/wiki/Camel_case). | ||
- `esModules` accepts a string or array of strings | ||
- `expectedOutput` is a path to the expected output. | ||
- `options` accept the same options as the CLI, but in [Camel Case](https://en.wikipedia.org/wiki/Camel_case). | ||
You can also pass the `--update` flag to update the expected outputs for failing tests. | ||
## API usage | ||
@@ -105,0 +106,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33695
337
128
4