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

Commitf7b73e5

Browse files
committed
feat: update package.json and create tarball inprepare hook
1 parent031281d commitf7b73e5

File tree

5 files changed

+122
-24
lines changed

5 files changed

+122
-24
lines changed

‎README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ Set of [semantic-release](https://github.com/semantic-release/semantic-release)
1313

1414
Verify the presence of the`NPM_TOKEN` environment variable, create or update the`.npmrc` file with the token and verify the token is valid.
1515

16-
##publish
16+
##prepare
17+
18+
Update the`package.json` version and[create](https://docs.npmjs.com/cli/pack) the`npm` package tarball.
1719

18-
Update the`package.json` version,[create](https://docs.npmjs.com/cli/pack) the`npm` package tarball and[publish](https://docs.npmjs.com/cli/publish) to the`npm` registry.
20+
##publish
21+
[Publish](https://docs.npmjs.com/cli/publish) to the`npm` registry.
1922

2023
##Configuration
2124

‎index.js‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ const setLegacyToken = require('./lib/set-legacy-token');
44
constgetPkg=require('./lib/get-pkg');
55
constverifyNpmConfig=require('./lib/verify-config');
66
constverifyNpmAuth=require('./lib/verify-auth');
7+
constprepareNpm=require('./lib/prepare');
78
constpublishNpm=require('./lib/publish');
89

910
letverified;
11+
letprepared;
1012

1113
asyncfunctionverifyConditions(pluginConfig,{options:{publish}, logger}){
1214
// 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
@@ -38,9 +40,30 @@ async function verifyConditions(pluginConfig, {options: {publish}, logger}) {
3840
verified=true;
3941
}
4042

43+
asyncfunctionprepare(pluginConfig,{nextRelease:{version}, logger}){
44+
letpkg;
45+
consterrors=verified ?[] :verifyNpmConfig(pluginConfig);
46+
47+
try{
48+
// Reload package.json in case a previous external step updated it
49+
pkg=awaitgetPkg(pluginConfig.pkgRoot);
50+
if(!verified&&pluginConfig.npmPublish!==false){
51+
setLegacyToken();
52+
awaitverifyNpmAuth(pluginConfig,pkg,logger);
53+
}
54+
}catch(err){
55+
errors.push(...err);
56+
}
57+
if(errors.length>0){
58+
thrownewAggregateError(errors);
59+
}
60+
awaitprepareNpm(pluginConfig,version,logger);
61+
prepared=true;
62+
}
63+
4164
asyncfunctionpublish(pluginConfig,{nextRelease:{version}, logger}){
4265
letpkg;
43-
consterrors=verifyNpmConfig(pluginConfig);
66+
consterrors=verified ?[] :verifyNpmConfig(pluginConfig);
4467

4568
setLegacyToken();
4669

@@ -56,7 +79,10 @@ async function publish(pluginConfig, {nextRelease: {version}, logger}) {
5679
if(errors.length>0){
5780
thrownewAggregateError(errors);
5881
}
82+
if(!prepared){
83+
awaitprepareNpm(pluginConfig,version,logger);
84+
}
5985
returnpublishNpm(pluginConfig,pkg,version,logger);
6086
}
6187

62-
module.exports={verifyConditions, publish};
88+
module.exports={verifyConditions,prepare,publish};

‎lib/prepare.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
constpath=require('path');
2+
const{move}=require('fs-extra');
3+
constexeca=require('execa');
4+
constupdatePackageVersion=require('./update-package-version');
5+
6+
module.exports=async({tarballDir, pkgRoot},version,logger)=>{
7+
constbasePath=pkgRoot||'.';
8+
awaitupdatePackageVersion(version,basePath,logger);
9+
10+
if(tarballDir){
11+
logger.log('Creating npm package version %s',version);
12+
consttarball=(awaitexeca.stdout('npm',['pack',`./${basePath}`])).split('\n').pop();
13+
awaitmove(tarball,path.join(tarballDir.trim(),tarball));
14+
}
15+
};

‎lib/publish.js‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
constpath=require('path');
2-
const{move}=require('fs-extra');
31
constexeca=require('execa');
42
constgetRegistry=require('./get-registry');
5-
constupdatePackageVersion=require('./update-package-version');
6-
constgetReleaseInfo=require('./get-release-info.js');
7-
8-
module.exports=async({npmPublish, tarballDir, pkgRoot},{publishConfig, name},version,logger)=>{
9-
constbasePath=pkgRoot||'.';
10-
constregistry=awaitgetRegistry(publishConfig,name);
11-
awaitupdatePackageVersion(version,basePath,logger);
12-
13-
if(tarballDir){
14-
logger.log('Creating npm package version %s',version);
15-
consttarball=(awaitexeca.stdout('npm',['pack',`./${basePath}`])).split('\n').pop();
16-
awaitmove(tarball,path.join(tarballDir.trim(),tarball));
17-
}
3+
constgetReleaseInfo=require('./get-release-info');
184

5+
module.exports=async({npmPublish, pkgRoot},{publishConfig, name},version,logger)=>{
196
if(npmPublish!==false){
7+
constbasePath=pkgRoot||'.';
8+
constregistry=awaitgetRegistry(publishConfig,name);
209
logger.log('Publishing version %s to npm registry',version);
2110
constshell=awaitexeca('npm',['publish',`./${basePath}`,'--registry',registry]);
2211
process.stdout.write(shell.stdout);
12+
returngetReleaseInfo(name,publishConfig,registry);
2313
}
24-
25-
returngetReleaseInfo(name,publishConfig,registry);
2614
};

‎test/integration.test.js‎

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ test.serial('Create the package and skip publish', async t => {
205205
{logger:t.context.logger,nextRelease:{version:'1.0.0'}}
206206
);
207207

208-
t.deepEqual(result,{name:'npm package (@latest dist-tag)',url:undefined});
208+
t.falsy(result);
209209
t.is((awaitreadJson('./package.json')).version,'1.0.0');
210210
t.true(awaitpathExists(`./tarball/${pkg.name}-1.0.0.tgz`));
211211
awaitt.throws(execa('npm',['view',pkg.name,'version']));
@@ -221,7 +221,7 @@ test.serial('Create the package and skip publish from a sub-directory', async t
221221
{logger:t.context.logger,nextRelease:{version:'1.0.0'}}
222222
);
223223

224-
t.deepEqual(result,{name:'npm package (@latest dist-tag)',url:undefined});
224+
t.falsy(result);
225225
t.is((awaitreadJson('./dist/package.json')).version,'1.0.0');
226226
t.true(awaitpathExists(`./tarball/${pkg.name}-1.0.0.tgz`));
227227
awaitt.throws(execa('npm',['view',pkg.name,'version']));
@@ -257,12 +257,78 @@ test.serial('Throw SemanticReleaseError Array if config option are not valid in
257257
t.is(errors[3].code,'ENOPKGNAME');
258258
});
259259

260-
test.serial('Verify token and set up auth only on the fist call',asynct=>{
260+
test.serial('Prepare the package',asynct=>{
261+
Object.assign(process.env,npmRegistry.authEnv);
262+
constpkg={name:'prepare',version:'0.0.0',publishConfig:{registry:npmRegistry.url}};
263+
awaitoutputJson('./package.json',pkg);
264+
265+
awaitt.context.m.prepare({},{logger:t.context.logger,nextRelease:{version:'1.0.0'}});
266+
267+
t.is((awaitreadJson('./package.json')).version,'1.0.0');
268+
t.false(awaitpathExists(`./${pkg.name}-1.0.0.tgz`));
269+
});
270+
271+
test.serial('Prepare the package from a sub-directory',asynct=>{
272+
Object.assign(process.env,npmRegistry.authEnv);
273+
constpkg={name:'prepare-sub-dir',version:'0.0.0',publishConfig:{registry:npmRegistry.url}};
274+
awaitoutputJson('./dist/package.json',pkg);
275+
276+
awaitt.context.m.prepare({pkgRoot:'dist'},{logger:t.context.logger,nextRelease:{version:'1.0.0'}});
277+
278+
t.is((awaitreadJson('./dist/package.json')).version,'1.0.0');
279+
t.false(awaitpathExists(`./${pkg.name}-1.0.0.tgz`));
280+
});
281+
282+
test.serial('Create the package in prepare step',asynct=>{
283+
constpkg={name:'prepare-pkg',version:'0.0.0',publishConfig:{registry:npmRegistry.url}};
284+
awaitoutputJson('./package.json',pkg);
285+
286+
awaitt.context.m.prepare(
287+
{npmPublish:false,tarballDir:'tarball'},
288+
{logger:t.context.logger,nextRelease:{version:'1.0.0'}}
289+
);
290+
291+
t.is((awaitreadJson('./package.json')).version,'1.0.0');
292+
t.true(awaitpathExists(`./tarball/${pkg.name}-1.0.0.tgz`));
293+
});
294+
295+
test.serial('Throw SemanticReleaseError Array if config option are not valid in prepare',asynct=>{
296+
constpkg={publishConfig:{registry:npmRegistry.url}};
297+
awaitoutputJson('./package.json',pkg);
298+
constnpmPublish=42;
299+
consttarballDir=42;
300+
constpkgRoot=42;
301+
302+
consterrors=[
303+
...(awaitt.throws(
304+
t.context.m.prepare(
305+
{npmPublish, tarballDir, pkgRoot},
306+
{
307+
options:{publish:['@semantic-release/github','@semantic-release/npm']},
308+
nextRelease:{version:'1.0.0'},
309+
logger:t.context.logger,
310+
}
311+
)
312+
)),
313+
];
314+
315+
t.is(errors[0].name,'SemanticReleaseError');
316+
t.is(errors[0].code,'EINVALIDNPMPUBLISH');
317+
t.is(errors[1].name,'SemanticReleaseError');
318+
t.is(errors[1].code,'EINVALIDTARBALLDIR');
319+
t.is(errors[2].name,'SemanticReleaseError');
320+
t.is(errors[2].code,'EINVALIDPKGROOT');
321+
t.is(errors[3].name,'SemanticReleaseError');
322+
t.is(errors[3].code,'ENOPKGNAME');
323+
});
324+
325+
test.serial('Verify token and set up auth only on the fist call, then prepare on prepare call only',asynct=>{
261326
Object.assign(process.env,npmRegistry.authEnv);
262327
constpkg={name:'test-module',version:'0.0.0-dev',publishConfig:{registry:npmRegistry.url}};
263328
awaitoutputJson('./package.json',pkg);
264329

265330
awaitt.notThrows(t.context.m.verifyConditions({},{options:{},logger:t.context.logger}));
331+
awaitt.context.m.prepare({},{logger:t.context.logger,nextRelease:{version:'1.0.0'}});
266332

267333
constresult=awaitt.context.m.publish({},{logger:t.context.logger,nextRelease:{version:'1.0.0'}});
268334
t.deepEqual(result,{name:'npm package (@latest dist-tag)',url:undefined});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp