Skip to content

Commit bd78515

Browse files
committed
NPA-9: TKLEING - Handle scoped npm packages
1 parent 795e1e2 commit bd78515

File tree

4 files changed

+39
-80
lines changed

4 files changed

+39
-80
lines changed

__tests__/index.test.js

+32-62
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const minimist = require(`minimist`);
22
const path = require(`path`);
3-
const shell = require(`shelljs`);
3+
const mockShell = jest.requireActual(`shelljs`);
44
const { mockShellFn } = require(path.join(process.cwd(), `mocks/shelljs.mocks`));
55
const pickBy = require(`lodash/pickBy`);
66
const keys = require(`lodash/keys`);
@@ -10,6 +10,22 @@ const map = require(`lodash/map`);
1010
const TEST_SUITE = `npm-pack-all: ${__filename}`;
1111
const TMP_DIR = path.join(process.cwd(), ".npm-pack-all-tmp");
1212

13+
beforeEach(() => {
14+
// mock shell commands
15+
jest.mock(`shelljs`, () => {
16+
return {
17+
code: 0, // success always
18+
config: { fatal: false, silent: false },
19+
exec: mockShellFn(`exec`),
20+
cp: mockShellFn(`cp`),
21+
mv: mockShellFn(`mv`),
22+
rm: mockShellFn(`rm`),
23+
mkdir: mockShellFn(`mkdir`),
24+
touch: mockShellFn(`touch`)
25+
};
26+
});
27+
});
28+
1329
beforeAll(() => {
1430
console.error = jest.fn();
1531
});
@@ -21,7 +37,7 @@ afterEach(() => {
2137
afterEach(() => {
2238
jest.resetAllMocks();
2339
jest.clearAllMocks();
24-
shell.rm(`-Rf`, `*.tgz`); // call this after post mock restore
40+
mockShell.rm(`-Rf`, `*.tgz`); // call this after post mock restore
2541
});
2642

2743
afterAll(() => {
@@ -47,20 +63,6 @@ describe(TEST_SUITE, () => {
4763
};
4864
});
4965

50-
// mock shell commands
51-
jest.mock(`shelljs`, () => {
52-
return {
53-
code: 0, // success always
54-
config: { fatal: false, silent: false },
55-
exec: mockShellFn(`exec`),
56-
cp: mockShellFn(`cp`),
57-
mv: mockShellFn(`mv`),
58-
rm: mockShellFn(`rm`),
59-
mkdir: mockShellFn(`mkdir`),
60-
touch: mockShellFn(`touch`)
61-
};
62-
});
63-
6466
// mock spawn
6567
jest.mock(`child_process`, () => {
6668
function on(eventType, callback) {
@@ -128,24 +130,11 @@ describe(TEST_SUITE, () => {
128130
};
129131
});
130132

131-
// mock shell commands
132-
jest.mock(`shelljs`, () => {
133-
return {
134-
code: 0, // success always
135-
config: { fatal: false, silent: false },
136-
cp: mockShellFn(`cp`),
137-
mv: mockShellFn(`mv`),
138-
rm: mockShellFn(`rm`),
139-
mkdir: mockShellFn(`mkdir`),
140-
touch: mockShellFn(`touch`)
141-
};
142-
});
143-
144133
// call script
145134
require(`../index`);
146135
});
147136

148-
test("Does output to --output flag location", () => {
137+
function outputTest() {
149138
const outputLoc = `deploy/artifact.tgz`;
150139
let mockArgs = `--output ${outputLoc}`;
151140
mockArgs = minimist(mockArgs.split(` `));
@@ -172,20 +161,6 @@ describe(TEST_SUITE, () => {
172161
};
173162
});
174163

175-
// mock shell commands
176-
jest.mock(`shelljs`, () => {
177-
return {
178-
code: 0, // success always
179-
config: { fatal: false, silent: false },
180-
exec: mockShellFn(`exec`),
181-
cp: mockShellFn(`cp`),
182-
mv: mockShellFn(`mv`),
183-
rm: mockShellFn(`rm`),
184-
mkdir: mockShellFn(`mkdir`),
185-
touch: mockShellFn(`touch`)
186-
};
187-
});
188-
189164
// have access to the mock shell
190165
const mockShell = require(`shelljs`);
191166

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

174+
const packageParse = path.posix.parse(packageJson.name);
175+
const scopedPrefix = packageParse.dir ? `${packageParse.dir}-`.replace("@", "") : "";
176+
const packageName = `${scopedPrefix}${packageParse.name}-${packageJson.version}.tgz`;
177+
199178
// these commands should be run in the following order by default
200179
expect(orderedArgs).toEqual([
201180
`rm("-Rf","${TMP_DIR}")`,
@@ -210,31 +189,22 @@ describe(TEST_SUITE, () => {
210189
`mv("-f","${TMP_DIR}/.npmignore","${path.join(process.cwd(), ".npmignore")}")`,
211190
`rm("-Rf","${TMP_DIR}")`,
212191
`mkdir("-p","${path.join(process.cwd(), "deploy")}")`,
213-
`mv("-f","${path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`)}","${path.join(
214-
process.cwd(),
215-
outputLoc
216-
)}")`
192+
`mv("-f","${path.join(process.cwd(), `${packageName}`)}","${path.join(process.cwd(), outputLoc)}")`
217193
]);
194+
}
195+
test("Does output to --output flag location", outputTest);
196+
test("Scoped package does output to --output flag location", () => {
197+
jest.mock("../package.json", () => {
198+
const actualPackageJson = jest.requireActual("../package.json");
199+
return Object.assign(actualPackageJson, { name: `@scoped/${actualPackageJson.name}` });
200+
});
201+
outputTest();
218202
});
219203

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

224-
// mock shell commands
225-
jest.mock(`shelljs`, () => {
226-
return {
227-
code: 0, // success always
228-
config: { fatal: false, silent: false },
229-
exec: mockShellFn(`exec`),
230-
cp: mockShellFn(`cp`),
231-
mv: mockShellFn(`mv`),
232-
rm: mockShellFn(`rm`),
233-
mkdir: mockShellFn(`mkdir`),
234-
touch: mockShellFn(`touch`)
235-
};
236-
});
237-
238208
// call script
239209
require(`../index`);
240210

index.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require(`path`);
44
const shell = require(`shelljs`);
55
const { spawn } = require("child_process");
66

7-
const { CliError, safetyDecorator } = require(path.join(__dirname, `./utils/utils.index`));
7+
const { safetyDecorator } = require(path.join(__dirname, `./utils/utils.index`));
88
const cp = safetyDecorator(shell.cp);
99
const mv = safetyDecorator(shell.mv);
1010

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

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

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

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

86-
console.info(
87-
`Moving ${path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`)} to ${path.join(
88-
process.cwd(),
89-
cliArgs.output
90-
)}`
91-
);
92-
shell.mv(
93-
`-f`,
94-
path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`),
95-
path.join(process.cwd(), cliArgs.output)
96-
);
86+
console.info(`Moving ${path.join(process.cwd(), packageName)} to ${path.join(process.cwd(), cliArgs.output)}`);
87+
shell.mv(`-f`, path.join(process.cwd(), packageName), path.join(process.cwd(), cliArgs.output));
9788
}
9889
}
9990

mocks/shelljs.mocks.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const defaultResult = {
66

77
module.exports.mockShellFn = function(fnName, result = defaultResult) {
88
return jest.fn(function() {
9-
console.info(` + shell.${fnName}(${String([...arguments])})`);
109
return result;
1110
});
1211
};

package-lock.json

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)