Skip to content

Commit bb47350

Browse files
m90mantoni
authored andcommitted
Publish to npm with public access when package is scoped and public
By default, npm assumes packages with a scoped name are private. Scoped package.jsons usually use the "private" field to signal they are expected to be public. When running `init` on such a package, make studio-changes pass `--access public` to npm publish so that the expected permissions are set.
1 parent 4483f04 commit bb47350

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

lib/init.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function addScript(scripts, name, source) {
1616
return false;
1717
}
1818

19+
function isScopedPublicPackage(pkg) {
20+
return (
21+
pkg.private === false &&
22+
typeof pkg.name === 'string' &&
23+
pkg.name.indexOf('@') === 0
24+
);
25+
}
26+
1927
module.exports = function (argv) {
2028
const json = fs.readFileSync('package.json', 'utf8');
2129
const pkg = JSON.parse(json);
@@ -49,13 +57,19 @@ module.exports = function (argv) {
4957
}
5058
}
5159
}
60+
61+
let postversion_script = 'git push --follow-tags && npm publish';
62+
if (isScopedPublicPackage(pkg)) {
63+
postversion_script += ' --access public';
64+
}
65+
5266
if (!has_commits && pkg.homepage) {
5367
version_script += ' --commits';
5468
}
5569

5670
addScript(scripts, 'preversion', 'npm test');
5771
addScript(scripts, 'version', version_script);
58-
addScript(scripts, 'postversion', 'git push --follow-tags && npm publish');
72+
addScript(scripts, 'postversion', postversion_script);
5973

6074
const indent = detectIndent(json).indent || ' ';
6175
const out = JSON.stringify(pkg, null, indent);

test/init-test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,53 @@ describe('init', () => {
299299
`
300300
);
301301
});
302+
303+
it('automatically passes `--access public` to scoped public packages', () => {
304+
fs.readFileSync
305+
.withArgs('package.json')
306+
.returns('{"name":"@studio/changes","private":false}');
307+
308+
const result = init();
309+
310+
assert.isTrue(result);
311+
assert.calledOnceWith(
312+
fs.writeFileSync,
313+
'package.json',
314+
`{
315+
"name": "@studio/changes",
316+
"private": false,
317+
"scripts": {
318+
"preversion": "${SCRIPT_PREVERSION}",
319+
"version": "${SCRIPT_VERSION}",
320+
"postversion": "${SCRIPT_POSTVERSION} --access public"
321+
}
322+
}
323+
`,
324+
'utf8'
325+
);
326+
});
327+
328+
it('does not pass `--access` to packages implicitly restricted by a scoped name', () => {
329+
fs.readFileSync
330+
.withArgs('package.json')
331+
.returns('{"name":"@acme-corp/ledger-tool"}');
332+
333+
const result = init();
334+
335+
assert.isTrue(result);
336+
assert.calledOnceWith(
337+
fs.writeFileSync,
338+
'package.json',
339+
`{
340+
"name": "@acme-corp/ledger-tool",
341+
"scripts": {
342+
"preversion": "${SCRIPT_PREVERSION}",
343+
"version": "${SCRIPT_VERSION}",
344+
"postversion": "${SCRIPT_POSTVERSION}"
345+
}
346+
}
347+
`,
348+
'utf8'
349+
);
350+
});
302351
});

0 commit comments

Comments
 (0)