Skip to content

Commit e76a4c3

Browse files
authored
ci(scripts): add scripts/npm-publish.ts (#57)
* ci: fix `npm run publish`-script * ci(scripts): add `scripts/npm-publish.ts`
1 parent 0b3612a commit e76a4c3

8 files changed

+110
-5
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"release:patch": "npm run release -- --release-as patch",
4343
"release:minor": "npm run release -- --release-as minor",
4444
"release:major": "npm run release -- --release-as major",
45-
"publish": "npm run build:all && npm publish:all"
45+
"publish": "nx run-many --target build --all --exclude=demo && nx run-many --target publish --all"
4646
}
4747
```
4848

@@ -64,8 +64,9 @@ automatically generated on releases by
6464

6565
https://stackblitz.com/github/[User|Organization]/[Repository]/tree/main/projects/demo
6666

67-
- [x] You can add more libraries using the same `npm run add` command to create a whole Angular Workspace with multiple
68-
libraries. Versioning and publishing is configured that they are released simultaneously like Angular packages.
67+
- [x] You can add more libraries using the same [Nx-generators](https://nx.dev/plugin-features/use-code-generators) to
68+
create a whole Angular Workspace with multiple libraries. Versioning and publishing is configured that they are
69+
released simultaneously like Angular packages.
6970

7071
## Infrastructure
7172

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"release:patch": "npm run release -- --release-as patch",
2727
"release:minor": "npm run release -- --release-as minor",
2828
"release:major": "npm run release -- --release-as major",
29-
"publish": "npm run build:all && npm run publish:all"
29+
"publish": "nx run-many --target build --all --exclude=demo && nx run-many --target publish --all"
3030
},
3131
"description": "This is a template project to develop, create and open-source Angular libraries with demos",
3232
"keywords": [

scripts/helpers/argv.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as process from 'process';
2+
3+
import {processLog} from './colored-log';
4+
5+
export function getValueByFlag<T extends string>(flag: string, fallback: T): T {
6+
const index = findIndexFlag(flag);
7+
8+
if (index === -1) {
9+
return fallback;
10+
}
11+
12+
const [parsedFlag, parsedValue] = process.argv[index].split(`=`) ?? [];
13+
const value =
14+
stringifier(parsedValue) ??
15+
(process.argv[index + 1].startsWith(`-`)
16+
? fallback
17+
: stringifier(process.argv[index + 1]) ?? fallback);
18+
19+
processLog(`parsed flags: \n${[parsedFlag, value].join(`=`)}`);
20+
21+
return value as T;
22+
}
23+
24+
export function hasFlag(flag: string): boolean {
25+
return findIndexFlag(flag) !== -1;
26+
}
27+
28+
export function findIndexFlag(flag: string): number {
29+
return process.argv.findIndex(arg => arg === flag || arg.split(`=`)[0] === flag);
30+
}
31+
32+
export function stringifier(value?: string): string | undefined {
33+
return value === `undefined` || value === `null` ? undefined : value;
34+
}

scripts/helpers/colored-log.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export function processLog(message: string): void {
2+
console.info('\x1b[36m%s\x1b[0m', message);
3+
}
4+
5+
export function errorLog(message: string): void {
6+
console.info('\x1B[31m%s\x1B[0m', message);
7+
}
8+
9+
export function successLog(message: string): void {
10+
console.info('\x1B[32m%s\x1B[0m', message);
11+
}
12+
13+
export function infoLog(message: string): void {
14+
console.info('\x1B[34m%s\x1B[0m', message);
15+
}
16+
17+
export function titleLog(message: string): void {
18+
console.info('\x1b[35m', message);
19+
}

scripts/helpers/get-all-versions.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {execSync} from 'child_process';
2+
3+
export function getAllVersions(name: string): string[] {
4+
return JSON.parse(
5+
execSync(`npm view ${name} versions --json || echo "[]"`).toString(),
6+
);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getLastMajorVersion(versions: string[], currentMajor: number): number {
2+
return Math.max(...versions.map(x => parseInt(x)), currentMajor);
3+
}

scripts/npm-publish.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {execSync} from 'child_process';
2+
import {resolve} from 'path';
3+
4+
import {getValueByFlag} from './helpers/argv';
5+
import {errorLog, infoLog, processLog, successLog} from './helpers/colored-log';
6+
import {getAllVersions} from './helpers/get-all-versions';
7+
import {getLastMajorVersion} from './helpers/get-last-major-version';
8+
9+
const isDryRun =
10+
getValueByFlag<'true' | 'false' | 'undefined'>(`--dry-run`, `false`) === `true`;
11+
const path = getValueByFlag<string>(`--path`, ``);
12+
13+
(async function main(): Promise<void> {
14+
const packageJson = await import(resolve(path, `package.json`));
15+
const versions: string[] = getAllVersions(packageJson.name);
16+
17+
if (versions.includes(packageJson.version) && !isDryRun) {
18+
errorLog(`${packageJson.name}@${packageJson.version} is already published`);
19+
20+
return;
21+
}
22+
23+
infoLog(`name: ${packageJson.name}`);
24+
infoLog(`version: ${packageJson.version}`);
25+
26+
const dry = isDryRun ? `--dry-run` : ``;
27+
const tag = makeTag(packageJson.version, versions);
28+
const command = `npm publish ${path} ${tag} ${dry} --access public`;
29+
30+
processLog(command);
31+
execSync(command, {stdio: `inherit`, encoding: `utf8`});
32+
successLog(`+${packageJson.name}@${packageJson.version} is published successfully`);
33+
})();
34+
35+
function makeTag(version: string, versions: string[]): string {
36+
const currentMajor = parseInt(version);
37+
const maxMajorVersion = getLastMajorVersion(versions, currentMajor);
38+
const tagFlag = maxMajorVersion > currentMajor ? `--tag v${currentMajor}-lts` : ``;
39+
40+
return version.includes(`rc`) ? `--tag next` : tagFlag;
41+
}

tsconfig.eslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"strict": false,
77
"incremental": true
88
},
9-
"include": ["projects"],
9+
"include": ["projects", "scripts"],
1010
"exclude": ["**/node_modules", "**/schematics/**", "**/.*/", "*.js"]
1111
}

0 commit comments

Comments
 (0)