Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite36a56b

Browse files
committed
feat: addpkgRoot option to publish a sub-directory
1 parentabcc70b commite36a56b

File tree

11 files changed

+240
-71
lines changed

11 files changed

+240
-71
lines changed

‎README.md‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ Use either `NPM_TOKEN` for token authentication or `NPM_USERNAME`, `NPM_PASSWORD
3939

4040
###Options
4141

42-
| Options| Description| Default|
43-
| ------------| ----------------------------------------------------------------------------------------------------------------------| -------|
44-
|`npmPublish`| Whether to publish the`npm` package to the registry. If`false` the`package.json` version will still be updated.|`true`|
45-
|`tarballDir`| Directory path in which to generate the the package tarball. If`false` the tarball is not be kept on the file system.|`false`|
42+
| Options| Description| Default|
43+
|--------------|---------------------------------------------------------------------------------------------------------------------|---------|
44+
|`npmPublish`| Whether to publish the`npm` package to the registry. If`false` the`package.json` version will still be updated.|`true`|
45+
|`pkgRoot`| Directory path to publish.|`.`|
46+
|`tarballDir`| Directory path in which to write the the package tarball. If`false` the tarball is not be kept on the file system.|`false`|
47+
48+
**Note**: The`pkgRoot` directory must contains a`package.json`. The version will be updated only in the`package.json` and`npm-shrinkwrap.json` within the`pkgRoot` directory.
4649

4750
###Npm configuration
4851

@@ -74,7 +77,7 @@ Each individual plugin can be disabled, replaced or used with other plugins in t
7477
}
7578
```
7679

77-
The`npmPublish` and`tarballDir` option can be used to skip the publishing to the`npm` registry and instead, release the package tarball with another plugin. For example with the[github](https://github.com/semantic-release/github):
80+
The`npmPublish` and`tarballDir` option can be used to skip the publishing to the`npm` registry and instead, release the package tarball with another plugin. For example with the[github](https://github.com/semantic-release/github) plugin:
7881

7982
```json
8083
{
@@ -95,3 +98,27 @@ The `npmPublish` and `tarballDir` option can be used to skip the publishing to t
9598
}
9699
}
97100
```
101+
102+
When publishing from a sub-directory with the`pkgRoot` option, the`package.json` and`npm-shrinkwrap.json` updated with the new version can be moved to another directory with a`postpublish`[npm script](https://docs.npmjs.com/misc/scripts). For example with the[git](https://github.com/semantic-release/git) plugin:
103+
104+
```json
105+
{
106+
"release": {
107+
"verifyConditions": ["@semantic-release/conditions-travis","@semantic-release/npm","@semantic-release/git"],
108+
"getLastRelease":"@semantic-release/npm",
109+
"publish": [
110+
{
111+
"path":"@semantic-release/npm",
112+
"pkgRoot":"dist"
113+
},
114+
{
115+
"path":"@semantic-release/git",
116+
"assets": ["package.json","npm-shrinkwrap.json"]
117+
},
118+
]
119+
},
120+
"scripts": {
121+
"postpublish":"cp -r dist/package.json . && cp -r dist/npm-shrinkwrap.json ."
122+
}
123+
}
124+
```

‎index.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getLastReleaseNpm = require('./lib/get-last-release');
88
letverified;
99

1010
asyncfunctionverifyConditions(pluginConfig,{options, logger}){
11-
// If the npm publish plugin is used and has `npmPublish`or `tarballDir` configured, validate them now in order to prevent any release if the configuration is wrong
11+
// If the npm publish plugin is used and has `npmPublish`, `tarballDir`or `pkgRoot` configured, validate them now in order to prevent any release if the configuration is wrong
1212
if(options.publish){
1313
constpublishPlugin=castArray(options.publish).find(
1414
config=>config.path&&config.path==='@semantic-release/npm'
@@ -19,18 +19,29 @@ async function verifyConditions(pluginConfig, {options, logger}) {
1919
if(publishPlugin&&publishPlugin.tarballDir){
2020
pluginConfig.tarballDir=publishPlugin.tarballDir;
2121
}
22+
if(publishPlugin&&publishPlugin.pkgRoot){
23+
pluginConfig.pkgRoot=publishPlugin.pkgRoot;
24+
}
2225
}
2326
setLegacyToken();
24-
constpkg=awaitgetPkg();
27+
constpkg=awaitgetPkg(pluginConfig.pkgRoot);
2528
awaitverifyNpm(pluginConfig,pkg,logger);
2629
verified=true;
2730
}
2831

29-
asyncfunctiongetLastRelease(pluginConfig,{logger}){
32+
asyncfunctiongetLastRelease(pluginConfig,{options,logger}){
3033
setLegacyToken();
3134
// Reload package.json in case a previous external step updated it
32-
constpkg=awaitgetPkg();
35+
constpkg=awaitgetPkg(pluginConfig.pkgRoot);
3336
if(!verified){
37+
if(options.publish){
38+
constpublishPlugin=castArray(options.publish).find(
39+
config=>config.path&&config.path==='@semantic-release/npm'
40+
);
41+
if(publishPlugin&&publishPlugin.pkgRoot){
42+
pluginConfig.pkgRoot=publishPlugin.pkgRoot;
43+
}
44+
}
3445
awaitverifyNpm(pluginConfig,pkg,logger);
3546
verified=true;
3647
}
@@ -40,7 +51,7 @@ async function getLastRelease(pluginConfig, {logger}) {
4051
asyncfunctionpublish(pluginConfig,{nextRelease:{version}, logger}){
4152
setLegacyToken();
4253
// Reload package.json in case a previous external step updated it
43-
constpkg=awaitgetPkg();
54+
constpkg=awaitgetPkg(pluginConfig.pkgRoot);
4455
if(!verified){
4556
awaitverifyNpm(pluginConfig,pkg,logger);
4657
verified=true;

‎lib/get-pkg.js‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
constreadPkgUp=require('read-pkg-up');
1+
constreadPkg=require('read-pkg');
22
constSemanticReleaseError=require('@semantic-release/error');
33

4-
module.exports=async()=>{
5-
const{pkg}=awaitreadPkgUp();
4+
module.exports=asyncpkgRoot=>{
5+
try{
6+
constpkg=awaitreadPkg(pkgRoot);
7+
if(!pkg.name){
8+
thrownewSemanticReleaseError('No "name" found in package.json.','ENOPKGNAME');
9+
}
610

7-
if(!pkg){
8-
thrownewSemanticReleaseError('A package.json file is required to release on npm.','ENOPKG');
11+
returnpkg;
12+
}catch(err){
13+
if(err.code==='ENOENT'){
14+
thrownewSemanticReleaseError('A package.json file is required to release on npm.','ENOPKG');
15+
}
16+
throwerr;
917
}
10-
11-
if(!pkg.name){
12-
thrownewSemanticReleaseError('No "name" found in package.json.','ENOPKGNAME');
13-
}
14-
15-
returnpkg;
1618
};

‎lib/publish.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ const execa = require('execa');
44
constgetRegistry=require('./get-registry');
55
constupdatePackageVersion=require('./update-package-version');
66

7-
module.exports=async({npmPublish, tarballDir},{publishConfig, name},version,logger)=>{
7+
module.exports=async({npmPublish, tarballDir, pkgRoot},{publishConfig, name},version,logger)=>{
8+
constbasePath=pkgRoot||'.';
89
constregistry=awaitgetRegistry(publishConfig,name);
9-
awaitupdatePackageVersion(version,logger);
10+
awaitupdatePackageVersion(version,basePath,logger);
1011

1112
if(tarballDir){
1213
logger.log('Creating npm package version %s',version);
13-
consttarball=awaitexeca.stdout('npm',['pack']);
14+
consttarball=awaitexeca.stdout('npm',['pack',`./${basePath}`]);
1415
awaitmove(tarball,path.join(tarballDir.trim(),tarball));
1516
}
1617

1718
if(npmPublish!==false){
1819
logger.log('Publishing version %s to npm registry',version);
19-
constshell=awaitexeca('npm',['publish','--registry',registry]);
20+
constshell=awaitexeca('npm',['publish',`./${basePath}`,'--registry',registry]);
2021
process.stdout.write(shell.stdout);
2122
}
2223
};

‎lib/update-package-version.js‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
constpath=require('path');
12
const{readFile, writeFile, pathExists}=require('fs-extra');
23

3-
module.exports=async(version,logger)=>{
4-
constpkg=(awaitreadFile('./package.json')).toString();
4+
module.exports=async(version,basePath,logger)=>{
5+
constpackagePath=path.join(basePath,'package.json');
6+
constshrinkwrapPath=path.join(basePath,'npm-shrinkwrap.json');
7+
constpkg=(awaitreadFile(packagePath)).toString();
58

6-
awaitwriteFile('./package.json',replaceVersion(pkg,version));
7-
logger.log('Wrote version %s topackage.json',version);
9+
awaitwriteFile(packagePath,replaceVersion(pkg,version));
10+
logger.log('Wrote version %s to%s',version,packagePath);
811

9-
if(awaitpathExists('./npm-shrinkwrap.json')){
10-
constshrinkwrap=(awaitreadFile('./npm-shrinkwrap.json')).toString();
11-
awaitwriteFile('./npm-shrinkwrap.json',replaceVersion(shrinkwrap,version));
12-
logger.log('Wrote version %s tonpm-shrinkwrap.json',version);
12+
if(awaitpathExists(shrinkwrapPath)){
13+
constshrinkwrap=(awaitreadFile(shrinkwrapPath)).toString();
14+
awaitwriteFile(shrinkwrapPath,replaceVersion(shrinkwrap,version));
15+
logger.log('Wrote version %s to%s',version,shrinkwrapPath);
1316
}
1417
};
1518

‎lib/verify.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const SemanticReleaseError = require('@semantic-release/error');
44
constgetRegistry=require('./get-registry');
55
constsetNpmrcAuth=require('./set-npmrc-auth');
66

7-
module.exports=async({npmPublish, tarballDir},pkg,logger)=>{
7+
module.exports=async({npmPublish, tarballDir, pkgRoot},pkg,logger)=>{
88
if(!isUndefined(npmPublish)&&!isBoolean(npmPublish)){
99
thrownewSemanticReleaseError('The "npmPublish" options, if defined, must be a Boolean.','EINVALIDNPMPUBLISH');
1010
}
@@ -13,6 +13,10 @@ module.exports = async ({npmPublish, tarballDir}, pkg, logger) => {
1313
thrownewSemanticReleaseError('The "tarballDir" options, if defined, must be a String.','EINVALIDTARBALLDIR');
1414
}
1515

16+
if(!isUndefined(pkgRoot)&&!isString(pkgRoot)){
17+
thrownewSemanticReleaseError('The "pkgRoot" options, if defined, must be a String.','EINVALIDPKGROOT');
18+
}
19+
1620
constregistry=awaitgetRegistry(pkg.publishConfig,pkg.name);
1721
awaitsetNpmrcAuth(registry,logger);
1822
try{

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nerf-dart":"^1.0.0",
2424
"npm-conf":"^1.1.3",
2525
"npm-registry-client":"^8.5.0",
26-
"read-pkg-up":"^3.0.0",
26+
"read-pkg":"^3.0.0",
2727
"registry-auth-token":"^3.3.1"
2828
},
2929
"devDependencies": {

‎test/get-pkg.test.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
importtestfrom'ava';
2-
import{writeJson,writeFile}from'fs-extra';
2+
import{outputJson,writeFile}from'fs-extra';
33
importtempyfrom'tempy';
44
importgetPkgfrom'../lib/get-pkg';
55

@@ -18,13 +18,22 @@ test.afterEach.always(() => {
1818

1919
test.serial('Verify name and return parsed package.json',asynct=>{
2020
constpkg={name:'package',version:'0.0.0'};
21-
awaitwriteJson('./package.json',pkg);
21+
awaitoutputJson('./package.json',pkg);
2222

2323
constresult=awaitgetPkg();
2424
t.is(pkg.name,result.name);
2525
t.is(pkg.version,result.version);
2626
});
2727

28+
test.serial('Verify name and return parsed package.json from a sub-directory',asynct=>{
29+
constpkg={name:'package',version:'0.0.0'};
30+
awaitoutputJson('./dist/package.json',pkg);
31+
32+
constresult=awaitgetPkg('dist');
33+
t.is(pkg.name,result.name);
34+
t.is(pkg.version,result.version);
35+
});
36+
2837
test.serial('Throw error if missing package.json',asynct=>{
2938
consterror=awaitt.throws(getPkg());
3039

@@ -33,7 +42,7 @@ test.serial('Throw error if missing package.json', async t => {
3342
});
3443

3544
test.serial('Throw error if missing package name',asynct=>{
36-
awaitwriteJson('./package.json',{version:'0.0.0'});
45+
awaitoutputJson('./package.json',{version:'0.0.0'});
3746

3847
consterror=awaitt.throws(getPkg());
3948

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp