Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): update code to allow it to be executed #982

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions @packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
},
"license": "MIT",
"dependencies": {
"@commitlint/test": "^11.0.0",
"execa": "^5.0.0",
"is-builtin-module": "3.0.0",
"meow": "8.1.0",
"fs-extra": "^9.0.0",
"read-pkg": "5.2.0",
"require-from-string": "2.0.2",
"tar-fs": "2.1.1"
"tar-fs": "2.1.1",
"tmp": "0.2.1",
"yargs": "^16.0.0"
},
"gitHead": "71f0194f33943954a8dac1c458be47e5049717cd"
}
102 changes: 58 additions & 44 deletions @packages/utils/pkg-check.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const fs = require('fs-extra');
const zlib = require('zlib');

const execa = require('execa');
const meow = require('meow');
const yargs = require('yargs');
const readPkg = require('read-pkg');
const requireFromString = require('require-from-string');
const tar = require('tar-fs');
const {fix} = require('@commitlint/test');
const tmp = require('tmp');

const builtin = require.resolve('is-builtin-module');
armano2 marked this conversation as resolved.
Show resolved Hide resolved
tmp.setGracefulCleanup();

const PRELUDE = `
var _require = require;
var Module = require('module');
var originalLoader = Module._load

require = function(id) {
Module._load = function(path, parent) {
if (path.startsWith('.') || Module.builtinModules.includes(path)) {
return originalLoader.apply(this, arguments);
}
var dummy = new Proxy({}, {
get() {
return dummy;
}
});

var _isBuiltIn = _require('${builtin}');
if (id[0] === '.' || _isBuiltIn(id)) {
return _require(id);
} else {
return dummy;
}
return dummy;
armano2 marked this conversation as resolved.
Show resolved Hide resolved
};
`;

function main(cli) {
function main(flags) {
if (!Proxy) {
console
.warn('Skipping pkg-check, detected missing Proxy support')
.process.exit(0);
}

const cwd = cli.flags.cwd || process.cwd();
const cwd = flags.cwd || process.cwd();
const skipImport =
typeof cli.flags.skipImport === 'boolean' ? cli.flags.skipImport : false;
typeof flags.skipImport === 'boolean' ? flags.skipImport : false;

return readPkg({cwd}).then((pkg) => {
return getTarballFiles(cwd, {write: !skipImport}).then((tarball) => {
return getPackageFiles(cwd).then((pkgFiles) => {
let problems = [];

if (!cli.flags.skipBin) {
if (!flags.skipBin) {
problems = problems.concat(
pkgFiles.bin
.filter((binFile) => tarball.files.indexOf(binFile) === -1)
Expand All @@ -59,18 +57,15 @@ function main(cli) {
);
}

if (
!cli.flags.skipMain &&
tarball.files.indexOf(pkgFiles.main) === -1
) {
if (!flags.skipMain && tarball.files.indexOf(pkgFiles.main) === -1) {
problems.push({
type: 'main',
file: pkgFiles.main,
message: `Required main file ${pkgFiles.main} not found for ${pkg.name}`,
});
}

if (!cli.flags.skipImport && !cli.flags.skipMain) {
if (!flags.skipImport && !flags.skipMain) {
const importable = fileImportable(
path.join(tarball.dirname, pkgFiles.main)
);
Expand All @@ -95,19 +90,35 @@ function main(cli) {
}

main(
meow(`
pkg-check

Check if a package creates valid tarballs

Options
--skip-main Skip main checks
--skip-bin Skip bin checks
--skip-import Skip import smoke test

Examples
$ pkg-check
`)
yargs
.options({
cwd: {
description: 'directory to execute in',
type: 'string',
},
skipMain: {
default: false,
type: 'boolean',
description: 'Skip main checks',
},
skipBin: {
default: false,
type: 'boolean',
description: 'Skip bin checks',
},
skipImport: {
default: false,
type: 'boolean',
description: 'Skip import smoke test',
},
})
.scriptName('pkg-check')
.usage('pkg-check\n')
.usage('Check if a package creates valid tarballs')
.example('$0', '')
.help()
.version()
.strict().argv
)
.then((report) => {
if (report.problems.length > 0) {
Expand All @@ -125,16 +136,19 @@ main(
.catch((err) => {
setTimeout(() => {
throw err;
});
}, 0);
});

async function getTarballFiles(source, options) {
const tmpDir = tmp.dirSync({
keep: false,
unsafeCleanup: true,
});
const cwd = tmpDir.name;
const tarball = path.join(cwd, 'test-archive.tgz');
await execa('yarn', ['pack', '--filename', tarball], {cwd: source});
armano2 marked this conversation as resolved.
Show resolved Hide resolved

function getTarballFiles(source, options) {
return fix
.bootstrap(source)
.then((cwd) =>
execa('npm', ['pack'], {cwd}).then((cp) => path.join(cwd, cp.stdout))
)
.then((tarball) => getArchiveFiles(tarball, options));
return getArchiveFiles(tarball, options);
}

function getArchiveFiles(filePath, options) {
Expand Down Expand Up @@ -173,7 +187,7 @@ function getPackageFiles(source) {

function normalizeMainPath(mainPath) {
const norm = path.normalize(mainPath);
if (norm[norm.length - 1] === '/') {
if (norm[norm.length - 1] === path.sep) {
armano2 marked this conversation as resolved.
Show resolved Hide resolved
return `${norm}index.js`;
}
return norm;
Expand Down
Loading