Browse Source

Merge pull request #10558 from abpframework/ci/split-publish-script

CI: split publish script into two files
pull/10559/head
Bunyamin Coskuner 5 years ago
committed by GitHub
parent
commit
26bada10c8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      npm/ng-packs/scripts/package.json
  2. 82
      npm/ng-packs/scripts/publish.ts
  3. 10
      npm/package-update-script.js
  4. 4
      npm/package.json
  5. 51
      npm/publish-mvc.ps1
  6. 21
      npm/publish-ng.ps1
  7. 8
      npm/publish-utils.js
  8. 14
      npm/replace-with-tilde.js
  9. 8
      npm/scripts/package.json
  10. 82
      npm/scripts/validate-versions.ts
  11. 22
      npm/update-gulp.js

3
npm/ng-packs/scripts/package.json

@ -17,7 +17,8 @@
"execa": "^2.0.3", "execa": "^2.0.3",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"prompt-confirm": "^2.0.4" "prompt-confirm": "^2.0.4",
"semver": "^7.3.5"
}, },
"devDependencies": { "devDependencies": {
"@types/fs-extra": "^8.0.1", "@types/fs-extra": "^8.0.1",

82
npm/ng-packs/scripts/publish.ts

@ -1,7 +1,8 @@
import program from 'commander';
import execa from 'execa'; import execa from 'execa';
import fse from 'fs-extra'; import fse from 'fs-extra';
import program from 'commander';
import replaceWithPreview from './replace-with-preview'; import replaceWithPreview from './replace-with-preview';
const semverParse = require('semver/functions/parse');
program program
.option( .option(
@ -10,14 +11,16 @@ program
) )
.option('-r, --registry <registry>', 'target npm server registry') .option('-r, --registry <registry>', 'target npm server registry')
.option('-p, --preview', 'publishes with preview tag') .option('-p, --preview', 'publishes with preview tag')
.option('-r, --rc', 'publishes with next tag') .option('-sg, --skipGit', 'skips git push')
.option('-g, --skipGit', 'skips git push'); .option('-sv, --skipVersionValidation', 'skips version validation');
program.parse(process.argv); program.parse(process.argv);
(async () => { (async () => {
const versions = ['major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', 'prerelease']; const versions = ['major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', 'prerelease'];
const oldVersion = fse.readJSONSync('../lerna.version.json').version;
if (!program.nextVersion) { if (!program.nextVersion) {
console.error('Please provide a version with --nextVersion attribute'); console.error('Please provide a version with --nextVersion attribute');
process.exit(1); process.exit(1);
@ -26,41 +29,43 @@ program.parse(process.argv);
const registry = program.registry || 'https://registry.npmjs.org'; const registry = program.registry || 'https://registry.npmjs.org';
try { try {
await fse.remove('../dist'); await fse.remove('../dist/packages');
await execa('yarn', ['install', '--ignore-scripts'], { stdout: 'inherit', cwd: '../' }); await execa('yarn', ['install'], { stdout: 'inherit', cwd: '../' });
await fse.rename('../lerna.version.json', '../lerna.json'); await updateVersion(program.nextVersion);
await execa( if (!program.skipVersionValidation) {
'yarn', await execa(
[ 'yarn',
'lerna', [
'version', 'validate-versions',
program.nextVersion, '--compareVersion',
'--yes', program.nextVersion,
'--no-commit-hooks', '--path',
'--skip-git', '../ng-packs/packages',
'--force-publish', ],
], { stdout: 'inherit', cwd: '../../scripts' },
{ stdout: 'inherit', cwd: '../' }, );
); }
await fse.rename('../lerna.json', '../lerna.version.json');
await execa('yarn', ['replace-with-tilde']);
if (program.preview) await replaceWithPreview(program.nextVersion); if (program.preview) await replaceWithPreview(program.nextVersion);
await execa('yarn', ['build', '--noInstall', '--skipNgcc'], { stdout: 'inherit' }); await execa('yarn', ['build', '--noInstall', '--skipNgcc'], { stdout: 'inherit' });
await execa('yarn', ['build:schematics'], { stdout: 'inherit' }); await execa('yarn', ['build:schematics'], { stdout: 'inherit' });
} catch (error) {
console.error(error.stderr);
console.error('\n\nAn error has occurred! Rolling back the changed package versions.');
await updateVersion(oldVersion);
process.exit(1);
}
try {
await fse.rename('../lerna.publish.json', '../lerna.json'); await fse.rename('../lerna.publish.json', '../lerna.json');
let tag: string; let tag: string;
if (program.preview) tag = 'preview'; if (program.preview) tag = 'preview';
if (program.rc) tag = 'next'; else if (semverParse(program.nextVersion).prerelease?.length) tag = 'next';
await execa( await execa(
'yarn', 'yarn',
@ -72,7 +77,14 @@ program.parse(process.argv);
); );
await fse.rename('../lerna.json', '../lerna.publish.json'); await fse.rename('../lerna.json', '../lerna.publish.json');
} catch (error) {
console.error(error.stderr);
console.error('\n\nAn error has occurred while publishing to the NPM!');
await fse.rename('../lerna.json', '../lerna.publish.json');
process.exit(1);
}
try {
if (!program.preview && !program.skipGit) { if (!program.preview && !program.skipGit) {
await execa('git', ['add', '../packages/*', '../package.json', '../lerna.version.json'], { await execa('git', ['add', '../packages/*', '../package.json', '../lerna.version.json'], {
stdout: 'inherit', stdout: 'inherit',
@ -88,3 +100,17 @@ program.parse(process.argv);
process.exit(0); process.exit(0);
})(); })();
async function updateVersion(version: string) {
await fse.rename('../lerna.version.json', '../lerna.json');
await execa(
'yarn',
['lerna', 'version', version, '--yes', '--no-commit-hooks', '--skip-git', '--force-publish'],
{ stdout: 'inherit', cwd: '../' },
);
await fse.rename('../lerna.json', '../lerna.version.json');
await execa('yarn', ['replace-with-tilde']);
}

10
npm/package-update-script.js

@ -1,11 +1,13 @@
const glob = require('glob'); const glob = require('glob');
var path = require('path'); var path = require('path');
const childProcess = require('child_process'); const childProcess = require('child_process');
const { program } = require('commander'); const { program } = require('commander');
program.version('0.0.1'); program.version('0.0.1');
program.option('-r, --rc', 'whether version is rc'); program.option('-pr, --prerelase', 'whether version is prerelase');
program.option('-rg, --registry <registry>', 'target npm server registry') program.option('-rg, --registry <registry>', 'target npm server registry');
program.parse(process.argv); program.parse(process.argv);
const packages = (process.argv[3] || 'abp').split(',').join('|'); const packages = (process.argv[3] || 'abp').split(',').join('|');
@ -15,7 +17,7 @@ const check = (pkgJsonPath) => {
return childProcess return childProcess
.execSync( .execSync(
`ncu "/^@(${packages}).*$/" --packageFile ${pkgJsonPath} -u${ `ncu "/^@(${packages}).*$/" --packageFile ${pkgJsonPath} -u${
program.rc ? ' --target greatest' : '' program.prerelase ? ' --target newest' : ''
}${program.registry ? ` --registry ${program.registry}` : ''}` }${program.registry ? ` --registry ${program.registry}` : ''}`
) )
.toString(); .toString();
@ -40,4 +42,4 @@ glob(folder + '/**/package.json', {}, (er, files) => {
console.log(check(file)); console.log(check(file));
}); });
}); });

4
npm/package.json

@ -16,6 +16,8 @@
"dependencies": { "dependencies": {
"commander": "^6.0.0", "commander": "^6.0.0",
"execa": "^3.4.0", "execa": "^3.4.0",
"fs-extra": "^8.1.0" "fast-glob": "^3.2.7",
"fs-extra": "^8.1.0",
"semver": "^7.3.5"
} }
} }

51
npm/publish-mvc.ps1

@ -0,0 +1,51 @@
param(
[string]$Version,
[string]$Registry
)
yarn install
$NextVersion = $(node publish-utils.js --nextVersion)
$RootFolder = (Get-Item -Path "./" -Verbose).FullName
if (-Not $Version) {
$Version = $NextVersion;
}
if (-Not $Registry) {
$Registry = "https://registry.npmjs.org";
}
$PacksPublishCommand = "npm run lerna -- exec 'npm publish --registry $Registry'"
$IsPrerelase = $(node publish-utils.js --prerelase --customVersion $Version) -eq "true";
if ($IsPrerelase) {
$PacksPublishCommand = $PacksPublishCommand.Substring(0, $PacksPublishCommand.Length - 1) + " --tag next'"
}
$commands = (
"npm run lerna -- version $Version --yes --no-commit-hooks --skip-git --force-publish",
"yarn replace-with-tilde",
"cd scripts",
"yarn install",
"yarn validate-versions --compareVersion $Version --path ../packs",
"cd ..",
$PacksPublishCommand
)
foreach ($command in $commands) {
$timer = [System.Diagnostics.Stopwatch]::StartNew()
Write-Host $command
Invoke-Expression $command
if ($LASTEXITCODE -ne '0' -And $command -notlike '*cd *') {
Write-Host ("Process failed! " + $command)
Set-Location $RootFolder
exit $LASTEXITCODE
}
$timer.Stop()
$total = $timer.Elapsed
Write-Output "-------------------------"
Write-Output "$command command took $total (Hours:Minutes:Seconds.Milliseconds)"
Write-Output "-------------------------"
}

21
npm/publish.ps1 → npm/publish-ng.ps1

@ -3,7 +3,7 @@ param(
[string]$Registry [string]$Registry
) )
npm install yarn install
$NextVersion = $(node publish-utils.js --nextVersion) $NextVersion = $(node publish-utils.js --nextVersion)
$RootFolder = (Get-Item -Path "./" -Verbose).FullName $RootFolder = (Get-Item -Path "./" -Verbose).FullName
@ -17,32 +17,23 @@ if (-Not $Registry) {
} }
$NgPacksPublishCommand = "npm run publish-packages -- --nextVersion $Version --skipGit --registry $Registry" $NgPacksPublishCommand = "npm run publish-packages -- --nextVersion $Version --skipGit --registry $Registry"
$PacksPublishCommand = "npm run lerna -- exec 'npm publish --registry $Registry'"
$UpdateGulpCommand = "npm run update-gulp" $UpdateGulpCommand = "npm run update-gulp"
$UpdateNgPacksCommand = "yarn update --registry $Registry" $UpdateNgPacksCommand = "yarn update --registry $Registry"
$IsRc = $(node publish-utils.js --rc --customVersion $Version) -eq "true"; $IsPrerelase = $(node publish-utils.js --prerelase --customVersion $Version) -eq "true";
if ($IsPrerelase) {
$UpdateGulpCommand += " -- --prerelase"
if ($IsRc) { $UpdateNgPacksCommand += " --prerelase"
$NgPacksPublishCommand += " --rc"
$UpdateGulpCommand += " -- --rc"
$PacksPublishCommand = $PacksPublishCommand.Substring(0, $PacksPublishCommand.Length - 1) + " --tag next'"
$UpdateNgPacksCommand += " --rc"
} }
$commands = ( $commands = (
"npm run lerna -- version $Version --yes --no-commit-hooks --skip-git --force-publish",
"npm run replace-with-tilde",
$PacksPublishCommand,
$UpdateNgPacksCommand, $UpdateNgPacksCommand,
"cd ng-packs\scripts", "cd ng-packs\scripts",
"npm install", "yarn install",
$NgPacksPublishCommand, $NgPacksPublishCommand,
"cd ../../", "cd ../../",
"cd scripts", "cd scripts",
"yarn",
"yarn remove-lock-files", "yarn remove-lock-files",
"cd ..", "cd ..",
$UpdateGulpCommand $UpdateGulpCommand

8
npm/publish-utils.js

@ -1,16 +1,18 @@
const { program } = require('commander'); const { program } = require('commander');
const fse = require('fs-extra'); const fse = require('fs-extra');
const semverParse = require('semver/functions/parse');
program.version('0.0.1'); program.version('0.0.1');
program.option('-n, --nextVersion', 'version in common.props'); program.option('-n, --nextVersion', 'version in common.props');
program.option('-r, --rc', 'whether version is rc'); program.option('-pr, --prerelase', 'whether version is prerelase');
program.option('-cv, --customVersion <customVersion>', 'set exact version'); program.option('-cv, --customVersion <customVersion>', 'set exact version');
program.parse(process.argv); program.parse(process.argv);
if (program.nextVersion) console.log(getVersion()); if (program.nextVersion) console.log(getVersion());
if (program.rc) console.log(getVersion().includes('rc')); if (program.prerelase)
console.log(!!semverParse(getVersion()).prerelease?.length);
function getVersion() { function getVersion() {
if (program.customVersion) return program.customVersion; if (program.customVersion) return program.customVersion;
@ -20,4 +22,4 @@ function getVersion() {
const first = commonProps.indexOf(versionTag) + versionTag.length; const first = commonProps.indexOf(versionTag) + versionTag.length;
const last = commonProps.indexOf(versionEndTag); const last = commonProps.indexOf(versionEndTag);
return commonProps.substring(first, last); return commonProps.substring(first, last);
} }

14
npm/replace-with-tilde.js

@ -1,5 +1,5 @@
const glob = require('glob'); const glob = require("glob");
const fse = require('fs-extra'); const fse = require("fs-extra");
function replace(filePath) { function replace(filePath) {
const pkg = fse.readJsonSync(filePath); const pkg = fse.readJsonSync(filePath);
@ -9,20 +9,20 @@ function replace(filePath) {
if (!dependencies) return; if (!dependencies) return;
Object.keys(dependencies).forEach((key) => { Object.keys(dependencies).forEach((key) => {
if (key.includes('@abp/') && key !== '@abp/utils') { if (key.includes("@abp/")) {
dependencies[key] = dependencies[key].replace('^', '~'); dependencies[key] = dependencies[key].replace("^", "~");
} }
}); });
fse.writeJsonSync(filePath, { ...pkg, dependencies }, { spaces: 2 }); fse.writeJsonSync(filePath, { ...pkg, dependencies }, { spaces: 2 });
} }
glob('./packs/**/package.json', {}, (er, files) => { glob("./packs/**/package.json", {}, (er, files) => {
files.forEach((path) => { files.forEach((path) => {
if (path.includes('node_modules')) { if (path.includes("node_modules")) {
return; return;
} }
replace(path); replace(path);
}); });
}); });

8
npm/scripts/package.json

@ -3,12 +3,14 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"scripts": { "scripts": {
"remove-lock-files": "ts-node -r tsconfig-paths/register remove-lock-files.ts" "remove-lock-files": "ts-node -r tsconfig-paths/register remove-lock-files.ts",
"validate-versions": "ts-node -r tsconfig-paths/register validate-versions.ts"
}, },
"dependencies": { "dependencies": {
"axios": "^0.24.0",
"chalk": "^4.1.0", "chalk": "^4.1.0",
"commander": "^6.1.0", "commander": "^8.3.0",
"execa": "^4.0.3", "execa": "^5.1.1",
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"glob": "^7.1.6", "glob": "^7.1.6",
"npm-check-updates": "^9.0.1", "npm-check-updates": "^9.0.1",

82
npm/scripts/validate-versions.ts

@ -0,0 +1,82 @@
import { program } from 'commander';
import fse from 'fs-extra';
import * as path from 'path';
import { log } from './utils/log';
let excludedPackages = [];
(async () => {
initCommander();
await compare();
})();
function initCommander() {
program.requiredOption(
'-v, --compareVersion <version>',
'version to compare'
);
program.requiredOption('-p, --path <path>', 'NPM packages folder path');
program.option(
'-ep, --excludedPackages <excludedpackages>',
'Packages that will not be checked. Can be passed with separeted comma (like @abp/utils,@abp/core)',
''
);
program.parse(process.argv);
excludedPackages = program.opts().excludedPackages.split(',');
}
async function compare() {
let { compareVersion, path: packagesPath } = program.opts();
packagesPath = path.resolve(packagesPath);
const packageFolders = await fse.readdir(packagesPath);
for (let i = 0; i < packageFolders.length; i++) {
const folder = packageFolders[i];
const pkgJsonPath = `${packagesPath}/${folder}/package.json`;
let pkgJson;
try {
pkgJson = await fse.readJSON(pkgJsonPath);
} catch (error) {}
if (
!excludedPackages.includes(pkgJson.name) &&
pkgJson.version !== compareVersion
) {
throwError(pkgJsonPath, pkgJson.name);
}
const { dependencies, peerDependencies } = pkgJson;
if (dependencies) await compareDependencies(dependencies, pkgJsonPath);
// if (peerDependencies) { // TODO: update peerDependencies while updating version
// await compareDependencies(peerDependencies, pkgJsonPath);
// }
}
}
async function compareDependencies(
dependencies: Record<string, string>,
filePath: string
) {
const { compareVersion } = program.opts();
const entries = Object.entries(dependencies);
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (
!excludedPackages.includes(entry[0]) &&
entry[0].match(/@(abp|volo)/)?.length &&
entry[1] !== `~${compareVersion}`
) {
throwError(filePath, entry[0], `~${compareVersion}`);
}
}
}
function throwError(filePath: string, pkg: string, version?: string) {
const { compareVersion } = program.opts();
log.error(`${filePath}: ${pkg} version is not ${version || compareVersion}`);
process.exit(1);
}

22
npm/update-gulp.js

@ -1,4 +1,4 @@
const glob = require('glob'); const glob = require('fast-glob');
var path = require('path'); var path = require('path');
const childProcess = require('child_process'); const childProcess = require('child_process');
const execa = require('execa'); const execa = require('execa');
@ -6,7 +6,7 @@ const fse = require('fs-extra');
const { program } = require('commander'); const { program } = require('commander');
program.version('0.0.1'); program.version('0.0.1');
program.option('-r, --rc', 'whether version is rc'); program.option('-pr, --prerelase', 'whether version is prerelase');
program.parse(process.argv); program.parse(process.argv);
const gulp = (folderPath) => { const gulp = (folderPath) => {
@ -18,8 +18,9 @@ const gulp = (folderPath) => {
} }
try { try {
execa.sync(`yarn`, ['install'], { cwd: folderPath, stdio: 'inherit' }); fse.removeSync(`${folderPath}/wwwroot/libs`);
execa.sync(`yarn`, ['gulp'], { cwd: folderPath, stdio: 'inherit' }); execa.sync('yarn', ['install'], { cwd: folderPath, stdio: 'inherit' });
execa.sync('yarn', ['gulp'], { cwd: folderPath, stdio: 'inherit' });
} catch (error) { } catch (error) {
console.log('\x1b[31m', 'Error: ' + error.message); console.log('\x1b[31m', 'Error: ' + error.message);
} }
@ -30,7 +31,7 @@ const updatePackages = (pkgJsonPath) => {
const result = childProcess const result = childProcess
.execSync( .execSync(
`ncu "/^@abp.*$/" --packageFile ${pkgJsonPath} -u${ `ncu "/^@abp.*$/" --packageFile ${pkgJsonPath} -u${
program.rc ? ' --target greatest' : '' program.prerelase ? ' --target newest' : ''
}` }`
) )
.toString(); .toString();
@ -40,8 +41,9 @@ const updatePackages = (pkgJsonPath) => {
} }
}; };
console.time(); (async () => {
glob('../**/package.json', {}, (er, files) => { console.time();
let files = await glob('../**/package.json');
files = files.filter( files = files.filter(
(f) => (f) =>
f && f &&
@ -53,7 +55,9 @@ glob('../**/package.json', {}, (er, files) => {
files.forEach((file) => { files.forEach((file) => {
updatePackages(file); updatePackages(file);
gulp(file.replace('package.json', '')); const folderPath = file.replace('package.json', '');
gulp(folderPath);
}); });
console.timeEnd(); console.timeEnd();
}); })();
Loading…
Cancel
Save