Skip to content

Commit 765cade

Browse files
jeetissEunjae Lee
authored andcommitted
feat: add support for scoped packages (#451)
* add support for scoped and private packages * simplify scoped package detection * simplify custom publishComand condition * ask question about access only for scoped packages * use serialize-javascript instead of JSON.stringify
1 parent ce296a6 commit 765cade

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

packages/shipjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"inquirer": "7.0.0",
4747
"mkdirp": "^0.5.1",
4848
"prettier": "^1.18.2",
49+
"serialize-javascript": "^2.1.0",
4950
"shell-quote": "^1.7.2",
5051
"shipjs-lib": "0.10.0",
5152
"temp-write": "4.0.0"

packages/shipjs/src/flow/setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ async function setup({ help = false, dir = '.' }) {
2222
mainVersionFile,
2323
packagesToBump,
2424
packagesToPublish,
25+
isScoped,
26+
isPublic,
2527
} = await askQuestions({ dir });
2628
const outputs = [
2729
addDevDependencies({ dependencies: ['shipjs'], dir }),
2830
addScriptsToPackageJson({ dir }),
2931
await addShipConfig({
32+
isScoped,
33+
isPublic,
3034
baseBranch,
3135
releaseBranch,
3236
useMonorepo,

packages/shipjs/src/step/setup/addShipConfig.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import runStep from '../runStep';
21
import fs from 'fs';
32
import path from 'path';
3+
import serialize from 'serialize-javascript';
4+
import runStep from '../runStep';
45
import { runPrettier } from '../../helper';
56
import { info } from '../../color';
67
import { print } from '../../util';
78

89
export default async ({
10+
isScoped,
11+
isPublic,
912
baseBranch,
1013
releaseBranch,
1114
useMonorepo,
@@ -17,6 +20,10 @@ export default async ({
1720
await runStep({ title: 'Creating ship.config.js' }, async () => {
1821
const filePath = path.resolve(dir, 'ship.config.js');
1922
const json = {
23+
publishCommand:
24+
isScoped && isPublic
25+
? ({ defaultCommand }) => `${defaultCommand} --access public`
26+
: undefined,
2027
mergeStrategy:
2128
baseBranch === releaseBranch
2229
? {
@@ -35,10 +42,8 @@ export default async ({
3542
}
3643
: undefined,
3744
};
38-
fs.writeFileSync(
39-
filePath,
40-
`module.exports = ${JSON.stringify(json, null, 2)};`
41-
);
45+
46+
fs.writeFileSync(filePath, `module.exports = ${serialize(json)};`);
4247
await runPrettier({ filePath, dir });
4348

4449
return () => {

packages/shipjs/src/step/setup/askQuestions.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default async ({ dir }) =>
2727
packagesToPublish,
2828
} = await askMonorepo(dir);
2929

30+
const { isScoped, isPublic } = await askPackageAccess(dir);
31+
3032
return {
3133
baseBranch,
3234
releaseBranch,
@@ -37,6 +39,8 @@ export default async ({ dir }) =>
3739
mainVersionFile,
3840
packagesToBump,
3941
packagesToPublish,
42+
isScoped,
43+
isPublic,
4044
};
4145
});
4246

@@ -195,6 +199,25 @@ async function askMonorepo(dir) {
195199
return { useMonorepo, mainVersionFile, packagesToBump, packagesToPublish };
196200
}
197201

202+
async function askPackageAccess(dir) {
203+
const isScoped = isScopedPackage(getJson(dir, 'package.json').name);
204+
205+
if (!isScoped) {
206+
return { isScoped };
207+
}
208+
209+
const { isPublic } = await inquirer.prompt([
210+
{
211+
type: 'confirm',
212+
name: 'isPublic',
213+
message: 'Publish public package?',
214+
default: true,
215+
},
216+
]);
217+
218+
return { isScoped, isPublic };
219+
}
220+
198221
function detectMonorepo(dir) {
199222
if (fs.existsSync(path.resolve(dir, 'lerna.json'))) {
200223
return true;
@@ -258,3 +281,11 @@ function stringArrayValidator(answer) {
258281
return errorMessage;
259282
}
260283
}
284+
285+
function isScopedPackage(name) {
286+
try {
287+
return name[0] === '@' && name.indexOf('/') !== -1;
288+
} catch (err) {
289+
return false;
290+
}
291+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7470,6 +7470,11 @@ sentence-case@^2.1.0:
74707470
no-case "^2.2.0"
74717471
upper-case-first "^1.1.2"
74727472

7473+
serialize-javascript@^2.1.0:
7474+
version "2.1.0"
7475+
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-2.1.0.tgz#9310276819efd0eb128258bb341957f6eb2fc570"
7476+
integrity sha512-a/mxFfU00QT88umAJQsNWOnUKckhNCqOl028N48e7wFmo2/EHpTo9Wso+iJJCMrQnmFvcjto5RJdAHEvVhcyUQ==
7477+
74737478
set-blocking@^2.0.0, set-blocking@~2.0.0:
74747479
version "2.0.0"
74757480
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"

0 commit comments

Comments
 (0)