Skip to content

Commit

Permalink
Merge pull request #20 from kleingtm/NPA-9
Browse files Browse the repository at this point in the history
NPA-9: TKLEING - Handle scoped npm packages
  • Loading branch information
kleingtm committed Feb 10, 2021
2 parents 795e1e2 + bd78515 commit f8b192d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 80 deletions.
94 changes: 32 additions & 62 deletions __tests__/index.test.js
@@ -1,6 +1,6 @@
const minimist = require(`minimist`);
const path = require(`path`);
const shell = require(`shelljs`);
const mockShell = jest.requireActual(`shelljs`);
const { mockShellFn } = require(path.join(process.cwd(), `mocks/shelljs.mocks`));
const pickBy = require(`lodash/pickBy`);
const keys = require(`lodash/keys`);
Expand All @@ -10,6 +10,22 @@ const map = require(`lodash/map`);
const TEST_SUITE = `npm-pack-all: ${__filename}`;
const TMP_DIR = path.join(process.cwd(), ".npm-pack-all-tmp");

beforeEach(() => {
// mock shell commands
jest.mock(`shelljs`, () => {
return {
code: 0, // success always
config: { fatal: false, silent: false },
exec: mockShellFn(`exec`),
cp: mockShellFn(`cp`),
mv: mockShellFn(`mv`),
rm: mockShellFn(`rm`),
mkdir: mockShellFn(`mkdir`),
touch: mockShellFn(`touch`)
};
});
});

beforeAll(() => {
console.error = jest.fn();
});
Expand All @@ -21,7 +37,7 @@ afterEach(() => {
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
shell.rm(`-Rf`, `*.tgz`); // call this after post mock restore
mockShell.rm(`-Rf`, `*.tgz`); // call this after post mock restore
});

afterAll(() => {
Expand All @@ -47,20 +63,6 @@ describe(TEST_SUITE, () => {
};
});

// mock shell commands
jest.mock(`shelljs`, () => {
return {
code: 0, // success always
config: { fatal: false, silent: false },
exec: mockShellFn(`exec`),
cp: mockShellFn(`cp`),
mv: mockShellFn(`mv`),
rm: mockShellFn(`rm`),
mkdir: mockShellFn(`mkdir`),
touch: mockShellFn(`touch`)
};
});

// mock spawn
jest.mock(`child_process`, () => {
function on(eventType, callback) {
Expand Down Expand Up @@ -128,24 +130,11 @@ describe(TEST_SUITE, () => {
};
});

// mock shell commands
jest.mock(`shelljs`, () => {
return {
code: 0, // success always
config: { fatal: false, silent: false },
cp: mockShellFn(`cp`),
mv: mockShellFn(`mv`),
rm: mockShellFn(`rm`),
mkdir: mockShellFn(`mkdir`),
touch: mockShellFn(`touch`)
};
});

// call script
require(`../index`);
});

test("Does output to --output flag location", () => {
function outputTest() {
const outputLoc = `deploy/artifact.tgz`;
let mockArgs = `--output ${outputLoc}`;
mockArgs = minimist(mockArgs.split(` `));
Expand All @@ -172,20 +161,6 @@ describe(TEST_SUITE, () => {
};
});

// mock shell commands
jest.mock(`shelljs`, () => {
return {
code: 0, // success always
config: { fatal: false, silent: false },
exec: mockShellFn(`exec`),
cp: mockShellFn(`cp`),
mv: mockShellFn(`mv`),
rm: mockShellFn(`rm`),
mkdir: mockShellFn(`mkdir`),
touch: mockShellFn(`touch`)
};
});

// have access to the mock shell
const mockShell = require(`shelljs`);

Expand All @@ -196,6 +171,10 @@ describe(TEST_SUITE, () => {
const packageJsonLoc = require(`path`).join(process.cwd(), `package.json`);
const packageJson = require(packageJsonLoc);

const packageParse = path.posix.parse(packageJson.name);
const scopedPrefix = packageParse.dir ? `${packageParse.dir}-`.replace("@", "") : "";
const packageName = `${scopedPrefix}${packageParse.name}-${packageJson.version}.tgz`;

// these commands should be run in the following order by default
expect(orderedArgs).toEqual([
`rm("-Rf","${TMP_DIR}")`,
Expand All @@ -210,31 +189,22 @@ describe(TEST_SUITE, () => {
`mv("-f","${TMP_DIR}/.npmignore","${path.join(process.cwd(), ".npmignore")}")`,
`rm("-Rf","${TMP_DIR}")`,
`mkdir("-p","${path.join(process.cwd(), "deploy")}")`,
`mv("-f","${path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`)}","${path.join(
process.cwd(),
outputLoc
)}")`
`mv("-f","${path.join(process.cwd(), `${packageName}`)}","${path.join(process.cwd(), outputLoc)}")`
]);
}
test("Does output to --output flag location", outputTest);
test("Scoped package does output to --output flag location", () => {
jest.mock("../package.json", () => {
const actualPackageJson = jest.requireActual("../package.json");
return Object.assign(actualPackageJson, { name: `@scoped/${actualPackageJson.name}` });
});
outputTest();
});

test("Return .npmignore to proper state", () => {
const fs = jest.requireActual(`fs`);
const npmIgnoreContent = fs.existsSync(`.npmignore`) && fs.readFileSync(`.npmignore`);

// mock shell commands
jest.mock(`shelljs`, () => {
return {
code: 0, // success always
config: { fatal: false, silent: false },
exec: mockShellFn(`exec`),
cp: mockShellFn(`cp`),
mv: mockShellFn(`mv`),
rm: mockShellFn(`rm`),
mkdir: mockShellFn(`mkdir`),
touch: mockShellFn(`touch`)
};
});

// call script
require(`../index`);

Expand Down
21 changes: 6 additions & 15 deletions index.js
Expand Up @@ -4,7 +4,7 @@ const path = require(`path`);
const shell = require(`shelljs`);
const { spawn } = require("child_process");

const { CliError, safetyDecorator } = require(path.join(__dirname, `./utils/utils.index`));
const { safetyDecorator } = require(path.join(__dirname, `./utils/utils.index`));
const cp = safetyDecorator(shell.cp);
const mv = safetyDecorator(shell.mv);

Expand All @@ -21,9 +21,6 @@ console.info(`

// parse cli args
const cliArgs = require(`minimist`)(process.argv.slice(2));
if (typeof cliArgs.output !== `string` && cliArgs.output) {
throw new CliError(`--output`, cliArgs.output, `The \`--output\` flag requires a string filename`);
}
console.info(`CLI Args: ${JSON.stringify(cliArgs, null, 4)}\n`);

const packageJson = require(path.join(process.cwd(), `package.json`));
Expand Down Expand Up @@ -77,23 +74,17 @@ function setBundledDependencies(pj) {

function setArtifactName(args) {
if (args.output) {
const packageParse = path.posix.parse(packageJson.name);
const scopedPrefix = packageParse.dir ? `${packageParse.dir}-`.replace("@", "") : "";
const packageName = `${scopedPrefix}${packageParse.name}-${packageJson.version}.tgz`;
const outputDir = path.parse(path.join(process.cwd(), cliArgs.output)).dir;
if (outputDir && !fs.existsSync(outputDir)) {
console.info(`Creating directory ${outputDir}`);
shell.mkdir(`-p`, outputDir);
}

console.info(
`Moving ${path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`)} to ${path.join(
process.cwd(),
cliArgs.output
)}`
);
shell.mv(
`-f`,
path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`),
path.join(process.cwd(), cliArgs.output)
);
console.info(`Moving ${path.join(process.cwd(), packageName)} to ${path.join(process.cwd(), cliArgs.output)}`);
shell.mv(`-f`, path.join(process.cwd(), packageName), path.join(process.cwd(), cliArgs.output));
}
}

Expand Down
1 change: 0 additions & 1 deletion mocks/shelljs.mocks.js
Expand Up @@ -6,7 +6,6 @@ const defaultResult = {

module.exports.mockShellFn = function(fnName, result = defaultResult) {
return jest.fn(function() {
console.info(` + shell.${fnName}(${String([...arguments])})`);
return result;
});
};
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8b192d

Please sign in to comment.