@hubspot/npm-scripts 0.0.1-experimental.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -0
- package/bin/linking +4 -0
- package/package.json +61 -0
- package/src/constants/release.d.ts +18 -0
- package/src/constants/release.js +23 -0
- package/src/linking.d.ts +1 -0
- package/src/linking.js +73 -0
- package/src/release.d.ts +5 -0
- package/src/release.js +192 -0
- package/src/types.d.ts +30 -0
- package/src/types.js +1 -0
- package/src/utils/git.d.ts +2 -0
- package/src/utils/git.js +12 -0
- package/src/utils/logging.d.ts +7 -0
- package/src/utils/logging.js +18 -0
- package/src/utils/npm.d.ts +4 -0
- package/src/utils/npm.js +75 -0
- package/src/utils/process.d.ts +4 -0
- package/src/utils/process.js +4 -0
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Dev-Experience-Fe-Scripts
|
|
2
|
+
|
|
3
|
+
**A collection of scripts for internal HubSpot npm package development and management.**
|
|
4
|
+
|
|
5
|
+
## Tools
|
|
6
|
+
|
|
7
|
+
### Package Linking Tool (`hubspot-linking`)
|
|
8
|
+
|
|
9
|
+
The linking tool helps manage symlinked HubSpot packages in your development environment. It provides an interactive way to link and unlink local copies of `@hubspot` packages.
|
|
10
|
+
|
|
11
|
+
#### Usage
|
|
12
|
+
|
|
13
|
+
It can be used via npx
|
|
14
|
+
```bash
|
|
15
|
+
npx @hubspot/npm-scripts hubspot-linking
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Or installed as a part of the project and used in the package.json
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@hubspot/npm-scripts": "latest"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"local-link": "hubspot-linking"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Prerequisites
|
|
32
|
+
|
|
33
|
+
- Run `yarn link` from inside the local copy of any HubSpot package you want to make available for linking
|
|
34
|
+
- The tool looks for packages in `~/.config/yarn/link/@hubspot/`
|
|
35
|
+
|
|
36
|
+
#### What it does
|
|
37
|
+
|
|
38
|
+
1. Scans your project's `node_modules/@hubspot` directory for currently linked packages
|
|
39
|
+
2. Lists all available packages that can be linked (from your yarn link directory)
|
|
40
|
+
3. Presents a checkbox interface to select which packages to link/unlink
|
|
41
|
+
4. Automatically handles linking selected packages and unlinking deselected ones
|
|
42
|
+
5. Runs `yarn install --force` to ensure dependencies are properly resolved
|
|
43
|
+
|
|
44
|
+
### Release Script Builder (`buildReleaseScript`)
|
|
45
|
+
|
|
46
|
+
A utility function that creates a comprehensive npm release script with support for different release tags and semantic versioning.
|
|
47
|
+
|
|
48
|
+
#### Usage
|
|
49
|
+
|
|
50
|
+
Create a release script file (e.g., `scripts/release.ts`):
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { buildReleaseScript } from '@hubspot/npm-scripts';
|
|
54
|
+
import path from 'path';
|
|
55
|
+
|
|
56
|
+
buildReleaseScript({
|
|
57
|
+
packageJsonLocation: path.resolve('./package.json'),
|
|
58
|
+
buildHandlerOptions: {
|
|
59
|
+
repositoryUrl: 'https://github.com/your-org/your-repo',
|
|
60
|
+
mainBranch: 'main', // optional, defaults to 'master'
|
|
61
|
+
build: async () => {
|
|
62
|
+
// Optional custom build function
|
|
63
|
+
// If not provided, runs 'yarn build'
|
|
64
|
+
},
|
|
65
|
+
postLatestRelease: async () => {
|
|
66
|
+
// Optional function to run after latest releases
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Then add to your `package.json`:
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"scripts": {
|
|
77
|
+
"release": "tsx ./scripts/release.ts release"
|
|
78
|
+
},
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"registry": "https://registry.npmjs.org/"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### Release Commands
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Release a patch version to latest
|
|
89
|
+
yarn release -v=patch -t=latest
|
|
90
|
+
|
|
91
|
+
# Release a minor version to next (beta)
|
|
92
|
+
yarn release -v=minor -t=next
|
|
93
|
+
|
|
94
|
+
# Release an experimental version
|
|
95
|
+
yarn release -v=patch -t=experimental
|
|
96
|
+
|
|
97
|
+
# Dry run (test without publishing)
|
|
98
|
+
yarn release -v=patch -t=latest -d
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Parameters
|
|
102
|
+
|
|
103
|
+
- `-v, --versionIncrement`: SemVer increment type (`patch`, `minor`, `major`, `prerelease`)
|
|
104
|
+
- `-t, --tag`: Release tag (`latest`, `next`, `experimental`)
|
|
105
|
+
- `-d, --dryRun`: Run without actually publishing
|
|
106
|
+
|
|
107
|
+
#### Features
|
|
108
|
+
|
|
109
|
+
- **Branch protection**: Ensures releases are made from the correct branch
|
|
110
|
+
- **Version validation**: Checks local version against published versions
|
|
111
|
+
- **Interactive prompts**: Confirms release details and requires 2FA codes
|
|
112
|
+
- **Automatic tagging**: Creates git tags and pushes to repository
|
|
113
|
+
- **GitHub integration**: Opens PR and release pages automatically
|
|
114
|
+
- **Rollback support**: Cleans up on failure
|
|
115
|
+
|
package/bin/linking
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hubspot/npm-scripts",
|
|
3
|
+
"version": "0.0.1-experimental.0",
|
|
4
|
+
"description": "Scripts for working with npm packages in the HubSpot ecosystem",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"hubspot-linking": "bin/linking"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rm -rf dist && tsc && cp -r bin dist && cp package.json dist && cp README.md dist",
|
|
13
|
+
"lint:local": "eslint . && prettier --list-different './**/*.{ts,tsx,js,json}'",
|
|
14
|
+
"local-dev": "yarn build && cd dist && yarn link && cd .. && tsc -w",
|
|
15
|
+
"prettier:write": "prettier --write './**/*.{ts,js,json}'",
|
|
16
|
+
"release": "yarn tsx ./scripts/release.ts release",
|
|
17
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@inquirer/prompts": "^7.8.6",
|
|
21
|
+
"chalk": "^5.6.2",
|
|
22
|
+
"findup-sync": "^5.0.0",
|
|
23
|
+
"fs-extra": "^11.3.2",
|
|
24
|
+
"open": "^10.2.0",
|
|
25
|
+
"semver": "^7.7.2",
|
|
26
|
+
"yargs": "^18.0.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@eslint/js": "^9.36.0",
|
|
30
|
+
"@types/findup-sync": "^4.0.5",
|
|
31
|
+
"@types/fs-extra": "^11.0.4",
|
|
32
|
+
"@types/node": "^24.5.2",
|
|
33
|
+
"@types/semver": "^7.7.1",
|
|
34
|
+
"@types/yargs": "^17.0.33",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^8.44.1",
|
|
36
|
+
"@typescript-eslint/parser": "^8.44.1",
|
|
37
|
+
"eslint": "^9.36.0",
|
|
38
|
+
"eslint-plugin-import": "^2.32.0",
|
|
39
|
+
"globals": "^16.4.0",
|
|
40
|
+
"husky": "^4.3.8",
|
|
41
|
+
"lint-staged": "^16.2.1",
|
|
42
|
+
"prettier": "^3.6.2",
|
|
43
|
+
"tsx": "^4.20.5",
|
|
44
|
+
"typescript": "^5.6.2"
|
|
45
|
+
},
|
|
46
|
+
"lint-staged": {
|
|
47
|
+
"**/*.{js,ts,tsx,scss,css}": [
|
|
48
|
+
"prettier -l",
|
|
49
|
+
"eslint"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"husky": {
|
|
53
|
+
"hooks": {
|
|
54
|
+
"pre-commit": "lint-staged"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"registry": "https://registry.npmjs.org/"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const DEFAULT_MAIN_BRANCH = "master";
|
|
2
|
+
export declare const TAG: {
|
|
3
|
+
readonly LATEST: "latest";
|
|
4
|
+
readonly NEXT: "next";
|
|
5
|
+
readonly EXPERIMENTAL: "experimental";
|
|
6
|
+
};
|
|
7
|
+
export declare const INCREMENT: {
|
|
8
|
+
readonly PATCH: "patch";
|
|
9
|
+
readonly MINOR: "minor";
|
|
10
|
+
readonly MAJOR: "major";
|
|
11
|
+
readonly PRERELEASE: "prerelease";
|
|
12
|
+
};
|
|
13
|
+
export declare const VERSION_INCREMENT_OPTIONS: readonly ["patch", "minor", "major", "prerelease"];
|
|
14
|
+
export declare const TAG_OPTIONS: readonly ["latest", "next", "experimental"];
|
|
15
|
+
export declare const PRERELEASE_IDENTIFIER: {
|
|
16
|
+
readonly NEXT: "beta";
|
|
17
|
+
readonly EXPERIMENTAL: "experimental";
|
|
18
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const DEFAULT_MAIN_BRANCH = 'master';
|
|
2
|
+
export const TAG = {
|
|
3
|
+
LATEST: 'latest',
|
|
4
|
+
NEXT: 'next',
|
|
5
|
+
EXPERIMENTAL: 'experimental',
|
|
6
|
+
};
|
|
7
|
+
export const INCREMENT = {
|
|
8
|
+
PATCH: 'patch',
|
|
9
|
+
MINOR: 'minor',
|
|
10
|
+
MAJOR: 'major',
|
|
11
|
+
PRERELEASE: 'prerelease',
|
|
12
|
+
};
|
|
13
|
+
export const VERSION_INCREMENT_OPTIONS = [
|
|
14
|
+
INCREMENT.PATCH,
|
|
15
|
+
INCREMENT.MINOR,
|
|
16
|
+
INCREMENT.MAJOR,
|
|
17
|
+
INCREMENT.PRERELEASE,
|
|
18
|
+
];
|
|
19
|
+
export const TAG_OPTIONS = [TAG.LATEST, TAG.NEXT, TAG.EXPERIMENTAL];
|
|
20
|
+
export const PRERELEASE_IDENTIFIER = {
|
|
21
|
+
NEXT: 'beta',
|
|
22
|
+
EXPERIMENTAL: 'experimental',
|
|
23
|
+
};
|
package/src/linking.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/linking.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { execSync } from 'node:child_process';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { checkbox } from '@inquirer/prompts';
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import findup from 'findup-sync';
|
|
8
|
+
import { logger } from './utils/logging.js';
|
|
9
|
+
import { EXIT_CODES } from './utils/process.js';
|
|
10
|
+
const hubspotPackageNamespace = '@hubspot';
|
|
11
|
+
const packageJson = 'package.json';
|
|
12
|
+
function getSymLinksInDirectory(directory) {
|
|
13
|
+
return fs
|
|
14
|
+
.readdirSync(directory, { recursive: false })
|
|
15
|
+
.map((item) => item.toString())
|
|
16
|
+
.filter((item) => {
|
|
17
|
+
const stats = fs.lstatSync(path.join(directory, item));
|
|
18
|
+
return stats.isSymbolicLink() && !item.includes('.bin');
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function getPackageName(node_modules_dir, packages) {
|
|
22
|
+
return packages.map((item) => {
|
|
23
|
+
const { name } = fs.readJsonSync(path.join(node_modules_dir, item, packageJson));
|
|
24
|
+
return name;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const yarnLinkPath = path.join(os.homedir(), '.config', 'yarn', 'link', hubspotPackageNamespace);
|
|
28
|
+
(async () => {
|
|
29
|
+
const hubspotNodeModules = path.join(`node_modules`, hubspotPackageNamespace);
|
|
30
|
+
const node_modules = findup(hubspotNodeModules);
|
|
31
|
+
if (!node_modules) {
|
|
32
|
+
logger.error(`Could not find ${hubspotNodeModules} directory in the current directory or the parent directories.`);
|
|
33
|
+
process.exit(EXIT_CODES.ERROR);
|
|
34
|
+
}
|
|
35
|
+
const currentlyInstalledLinks = getPackageName(node_modules, getSymLinksInDirectory(node_modules));
|
|
36
|
+
const installablePackages = getPackageName(node_modules, getSymLinksInDirectory(yarnLinkPath));
|
|
37
|
+
const choices = installablePackages.map((choice) => {
|
|
38
|
+
return {
|
|
39
|
+
value: choice,
|
|
40
|
+
name: choice,
|
|
41
|
+
checked: currentlyInstalledLinks.includes(choice),
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
if (choices.length === 0) {
|
|
45
|
+
logger.log('No packages eligible for linking, to make a package eligible, run `yarn link` inside of the local copy of the package');
|
|
46
|
+
process.exit(EXIT_CODES.ERROR);
|
|
47
|
+
}
|
|
48
|
+
let packagesToLink = [];
|
|
49
|
+
try {
|
|
50
|
+
packagesToLink = await checkbox({
|
|
51
|
+
message: 'Which packages would you like to link? - To get packages to show in this list, run `yarn link` from inside the local copy that package.',
|
|
52
|
+
choices,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
if (e instanceof Error && e.name === 'ExitPromptError') {
|
|
57
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
58
|
+
}
|
|
59
|
+
logger.error(e);
|
|
60
|
+
process.exit(EXIT_CODES.ERROR);
|
|
61
|
+
}
|
|
62
|
+
packagesToLink.forEach((pkg) => {
|
|
63
|
+
logger.log(chalk.cyan(`\nLinking package: ${pkg}`));
|
|
64
|
+
execSync(`yarn link ${pkg}`, { stdio: 'inherit' });
|
|
65
|
+
});
|
|
66
|
+
currentlyInstalledLinks.forEach(pkg => {
|
|
67
|
+
if (!packagesToLink.includes(pkg)) {
|
|
68
|
+
logger.log(chalk.yellow(`\nUnlinking package: ${pkg}`));
|
|
69
|
+
execSync(`yarn unlink ${pkg}`, { stdio: 'inherit' });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
execSync('yarn install --force', { stdio: 'inherit' });
|
|
73
|
+
})();
|
package/src/release.d.ts
ADDED
package/src/release.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { exec as _exec } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
import yargs from 'yargs';
|
|
4
|
+
import semver from 'semver';
|
|
5
|
+
import open from 'open';
|
|
6
|
+
import { logger } from './utils/logging.js';
|
|
7
|
+
import { EXIT_CODES } from './utils/process.js';
|
|
8
|
+
import { DEFAULT_MAIN_BRANCH, INCREMENT, PRERELEASE_IDENTIFIER, TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS, } from './constants/release.js';
|
|
9
|
+
import { confirm, input } from '@inquirer/prompts';
|
|
10
|
+
import fs from 'fs-extra';
|
|
11
|
+
import { gitCleanup, getCurrentGitBranch } from './utils/git.js';
|
|
12
|
+
import { getDistTags, publish, updateNextTag } from './utils/npm.js';
|
|
13
|
+
const exec = promisify(_exec);
|
|
14
|
+
function buildHandler({ build, postLatestRelease, packageName, localVersion, registry, repositoryUrl, mainBranch, }) {
|
|
15
|
+
return async function handler({ versionIncrement, tag, dryRun, }) {
|
|
16
|
+
const branch = await getCurrentGitBranch();
|
|
17
|
+
const isExperimental = tag === TAG.EXPERIMENTAL;
|
|
18
|
+
const isDryRun = Boolean(dryRun);
|
|
19
|
+
if (isExperimental && branch === mainBranch) {
|
|
20
|
+
logger.error(`Releases to experimental tag cannot be published from the ${mainBranch} branch`);
|
|
21
|
+
process.exit(EXIT_CODES.ERROR);
|
|
22
|
+
}
|
|
23
|
+
else if (!isExperimental && branch !== mainBranch) {
|
|
24
|
+
logger.error(`Releases to latest and next tags can only be published from the ${mainBranch} branch`);
|
|
25
|
+
process.exit(EXIT_CODES.ERROR);
|
|
26
|
+
}
|
|
27
|
+
if (tag === TAG.LATEST && versionIncrement === INCREMENT.PRERELEASE) {
|
|
28
|
+
logger.error('Invalid release: cannot increment prerelease number on latest tag.');
|
|
29
|
+
process.exit(EXIT_CODES.ERROR);
|
|
30
|
+
}
|
|
31
|
+
const { next: currentNextTag, experimental: currentExperimentalTag } = await getDistTags(packageName);
|
|
32
|
+
if (!isExperimental && currentNextTag !== localVersion) {
|
|
33
|
+
logger.error(`Local package.json version ${localVersion} is out of sync with published version ${currentNextTag}`);
|
|
34
|
+
process.exit(EXIT_CODES.ERROR);
|
|
35
|
+
}
|
|
36
|
+
const currentVersion = isExperimental
|
|
37
|
+
? currentExperimentalTag
|
|
38
|
+
: localVersion;
|
|
39
|
+
const prereleaseIdentifier = isExperimental
|
|
40
|
+
? PRERELEASE_IDENTIFIER.EXPERIMENTAL
|
|
41
|
+
: PRERELEASE_IDENTIFIER.NEXT;
|
|
42
|
+
const incrementType = tag === TAG.LATEST || versionIncrement === INCREMENT.PRERELEASE
|
|
43
|
+
? versionIncrement
|
|
44
|
+
: `pre${versionIncrement}`;
|
|
45
|
+
const newVersion = semver.inc(currentVersion, incrementType, prereleaseIdentifier);
|
|
46
|
+
const tempBranch = `v${newVersion}-publish`;
|
|
47
|
+
if (!newVersion) {
|
|
48
|
+
logger.error('Error incrementing version.');
|
|
49
|
+
process.exit(EXIT_CODES.ERROR);
|
|
50
|
+
}
|
|
51
|
+
if (dryRun) {
|
|
52
|
+
logger.log('\nDRY RUN');
|
|
53
|
+
}
|
|
54
|
+
logger.log(`\nCurrent version: ${currentVersion}`);
|
|
55
|
+
logger.log(`New version to release: ${newVersion}`);
|
|
56
|
+
const shouldRelease = await confirm({
|
|
57
|
+
message: `Release version ${newVersion} on tag ${tag}?`,
|
|
58
|
+
});
|
|
59
|
+
if (!shouldRelease) {
|
|
60
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
61
|
+
}
|
|
62
|
+
if (branch === mainBranch && !dryRun) {
|
|
63
|
+
logger.log(`Creating a temporary branch '${tempBranch}' for the release`);
|
|
64
|
+
await exec(`git checkout -b ${tempBranch}`);
|
|
65
|
+
}
|
|
66
|
+
if (tag === TAG.LATEST &&
|
|
67
|
+
!localVersion.includes(PRERELEASE_IDENTIFIER.NEXT)) {
|
|
68
|
+
const proceedWithoutBetaRelease = await confirm({
|
|
69
|
+
message: `The current changes have not yet been released in beta. It's recommended to release and test all changes on the ${TAG.NEXT} tag before releasing them to ${TAG.LATEST}. Are you sure you want to proceed?`,
|
|
70
|
+
default: false,
|
|
71
|
+
});
|
|
72
|
+
if (!proceedWithoutBetaRelease) {
|
|
73
|
+
logger.log(`\nTo release your changes on the next tag, run \`yarn release -v=${versionIncrement} -t=next\``);
|
|
74
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
logger.log(`\nUpdating version to ${newVersion}...`);
|
|
78
|
+
if (isExperimental) {
|
|
79
|
+
await exec(`yarn version --no-git-tag-version --new-version ${newVersion}`);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
await exec(`yarn version --new-version ${newVersion}`);
|
|
83
|
+
}
|
|
84
|
+
logger.success('Version updated successfully \n');
|
|
85
|
+
if (build && typeof build === 'function') {
|
|
86
|
+
await build();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
await exec(`yarn build`);
|
|
90
|
+
}
|
|
91
|
+
let otp = '';
|
|
92
|
+
if (!isDryRun) {
|
|
93
|
+
otp = await input({
|
|
94
|
+
message: 'Enter your NPM one-time password:',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
logger.log('\nDry run: skipping one-time password entry');
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
await publish(tag, otp, isDryRun, registry);
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
logger.error('An error occurred while releasing the CLI. Correct the error and re-run `yarn build`.');
|
|
105
|
+
await gitCleanup(newVersion);
|
|
106
|
+
process.exit(EXIT_CODES.ERROR);
|
|
107
|
+
}
|
|
108
|
+
const gitCommand = `git push --atomic origin ${tempBranch} v${newVersion}`;
|
|
109
|
+
if (tag === TAG.LATEST) {
|
|
110
|
+
try {
|
|
111
|
+
await updateNextTag(packageName, newVersion, otp, isDryRun, registry);
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
logger.error(`An error occurred while updating the ${TAG.NEXT} tag. To finish this release, run the following commands:`);
|
|
115
|
+
logger.log(`npm dist-tag add ${packageName}@${newVersion} ${TAG.NEXT}`);
|
|
116
|
+
logger.log(gitCommand);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (isDryRun) {
|
|
120
|
+
await gitCleanup(newVersion);
|
|
121
|
+
logger.log(`\nDry run: skipping push to Github`);
|
|
122
|
+
logger.log('Dry run release finished successfully');
|
|
123
|
+
process.exit(EXIT_CODES.SUCCESS);
|
|
124
|
+
}
|
|
125
|
+
if (isExperimental) {
|
|
126
|
+
logger.log(`\nExperimental release: Skipping push to Github`);
|
|
127
|
+
// Reset the version back to the local version
|
|
128
|
+
await exec(`yarn version --no-git-tag-version --new-version ${localVersion}`);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
logger.log(`\nPushing changes to Github...`);
|
|
132
|
+
await exec(gitCommand);
|
|
133
|
+
logger.log(`Changes pushed successfully`);
|
|
134
|
+
await open(`${repositoryUrl}/compare/${mainBranch}...${tempBranch}`);
|
|
135
|
+
}
|
|
136
|
+
logger.success(`HubSpot CLI version ${newVersion} published successfully`);
|
|
137
|
+
logger.log(`https://www.npmjs.com/package/${packageName}?activeTab=versions`);
|
|
138
|
+
if (tag === TAG.LATEST) {
|
|
139
|
+
logger.log(`\nRemember to create a new release on Github!`);
|
|
140
|
+
await open(`${repositoryUrl}/releases/new`);
|
|
141
|
+
if (postLatestRelease && typeof postLatestRelease === 'function') {
|
|
142
|
+
await postLatestRelease();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
async function builder(yargs) {
|
|
148
|
+
return yargs.options({
|
|
149
|
+
versionIncrement: {
|
|
150
|
+
alias: 'v',
|
|
151
|
+
demandOption: true,
|
|
152
|
+
describe: 'SemVer increment type for the next release',
|
|
153
|
+
choices: VERSION_INCREMENT_OPTIONS,
|
|
154
|
+
},
|
|
155
|
+
tag: {
|
|
156
|
+
alias: 't',
|
|
157
|
+
demandOption: true,
|
|
158
|
+
describe: 'Tag for the next release',
|
|
159
|
+
choices: TAG_OPTIONS,
|
|
160
|
+
},
|
|
161
|
+
dryRun: {
|
|
162
|
+
alias: 'd',
|
|
163
|
+
describe: 'Run through the publish process without actually publishing',
|
|
164
|
+
type: 'boolean',
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
export function buildReleaseScript({ packageJsonLocation, buildHandlerOptions, }) {
|
|
169
|
+
try {
|
|
170
|
+
const packageJsonContents = fs.readJsonSync(packageJsonLocation);
|
|
171
|
+
if (!packageJsonContents.publishConfig?.registry) {
|
|
172
|
+
logger.error(`publishConfig.registry must be defined in ${packageJsonLocation}`);
|
|
173
|
+
}
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
175
|
+
yargs(process.argv.slice(2))
|
|
176
|
+
.scriptName('yarn')
|
|
177
|
+
.usage('Hubspot npm release script')
|
|
178
|
+
.command('release', `Create a new npm release of the ${packageJsonContents.name} with the specified version and tag`, builder, buildHandler({
|
|
179
|
+
...buildHandlerOptions,
|
|
180
|
+
packageName: packageJsonContents.name,
|
|
181
|
+
localVersion: packageJsonContents.version,
|
|
182
|
+
registry: packageJsonContents.publishConfig?.registry,
|
|
183
|
+
mainBranch: buildHandlerOptions.mainBranch || DEFAULT_MAIN_BRANCH,
|
|
184
|
+
}))
|
|
185
|
+
.version(false)
|
|
186
|
+
.help().argv;
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
logger.error(e);
|
|
190
|
+
process.exit(EXIT_CODES.ERROR);
|
|
191
|
+
}
|
|
192
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS } from './constants/release.js';
|
|
2
|
+
export type ReleaseArguments = {
|
|
3
|
+
versionIncrement: (typeof VERSION_INCREMENT_OPTIONS)[number];
|
|
4
|
+
tag: (typeof TAG_OPTIONS)[number];
|
|
5
|
+
dryRun?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export type DistTags = {
|
|
8
|
+
[TAG.LATEST]: string;
|
|
9
|
+
[TAG.NEXT]: string;
|
|
10
|
+
[TAG.EXPERIMENTAL]: string;
|
|
11
|
+
};
|
|
12
|
+
export type Tag = (typeof TAG_OPTIONS)[number];
|
|
13
|
+
export interface BuildReleaseScriptBase {
|
|
14
|
+
build?: () => Promise<void> | void;
|
|
15
|
+
postLatestRelease?: () => Promise<void> | void;
|
|
16
|
+
repositoryUrl: string;
|
|
17
|
+
mainBranch?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface BuildReleaseScriptOptions extends BuildReleaseScriptBase {
|
|
20
|
+
packageName: string;
|
|
21
|
+
localVersion: string;
|
|
22
|
+
registry: string;
|
|
23
|
+
}
|
|
24
|
+
export interface PackageJson {
|
|
25
|
+
name: string;
|
|
26
|
+
version: string;
|
|
27
|
+
publishConfig: {
|
|
28
|
+
registry: string;
|
|
29
|
+
};
|
|
30
|
+
}
|
package/src/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { TAG, } from './constants/release.js';
|
package/src/utils/git.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import { exec as _exec } from 'node:child_process';
|
|
3
|
+
const exec = promisify(_exec);
|
|
4
|
+
export async function getCurrentGitBranch() {
|
|
5
|
+
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD');
|
|
6
|
+
return stdout.trim();
|
|
7
|
+
}
|
|
8
|
+
export async function gitCleanup(newVersion) {
|
|
9
|
+
await exec(`git reset HEAD~`);
|
|
10
|
+
await exec(`git checkout .`);
|
|
11
|
+
await exec(`git tag -d v${newVersion}`);
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
export const logger = {
|
|
3
|
+
log(...args) {
|
|
4
|
+
console.log(...args);
|
|
5
|
+
},
|
|
6
|
+
info(...args) {
|
|
7
|
+
console.log(`${chalk.bold('[INFO]')}`, ...args);
|
|
8
|
+
},
|
|
9
|
+
success(...args) {
|
|
10
|
+
console.log(`${chalk.cyan('[SUCCESS]')}`, ...args);
|
|
11
|
+
},
|
|
12
|
+
error(...args) {
|
|
13
|
+
console.log(`${chalk.red('[ERROR]')}`, ...args);
|
|
14
|
+
},
|
|
15
|
+
table(...args) {
|
|
16
|
+
console.table(...args);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { DistTags, Tag } from '../types.js';
|
|
2
|
+
export declare function getDistTags(packageName: string): Promise<DistTags>;
|
|
3
|
+
export declare function publish(tag: Tag, otp: string, isDryRun: boolean, registry: string): Promise<void>;
|
|
4
|
+
export declare function updateNextTag(packageName: string, newVersion: string, otp: string, isDryRun: boolean, registry: string): Promise<void>;
|
package/src/utils/npm.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import { exec as _exec, spawn } from 'node:child_process';
|
|
3
|
+
import { EXIT_CODES } from './process.js';
|
|
4
|
+
import { TAG } from '../constants/release.js';
|
|
5
|
+
import { logger } from './logging.js';
|
|
6
|
+
const exec = promisify(_exec);
|
|
7
|
+
export async function getDistTags(packageName) {
|
|
8
|
+
const { stdout } = await exec(`npm view ${packageName} dist-tags --json`);
|
|
9
|
+
const distTags = stdout.trim();
|
|
10
|
+
return JSON.parse(distTags);
|
|
11
|
+
}
|
|
12
|
+
export async function publish(tag, otp, isDryRun, registry) {
|
|
13
|
+
logger.log(`\nPublishing to ${tag}...`);
|
|
14
|
+
logger.log('-'.repeat(50));
|
|
15
|
+
logger.log('\n');
|
|
16
|
+
const commandArgs = [
|
|
17
|
+
'publish',
|
|
18
|
+
'--tag',
|
|
19
|
+
tag,
|
|
20
|
+
'--registry',
|
|
21
|
+
registry,
|
|
22
|
+
'--otp',
|
|
23
|
+
otp,
|
|
24
|
+
];
|
|
25
|
+
if (isDryRun) {
|
|
26
|
+
commandArgs.push('--dry-run');
|
|
27
|
+
}
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
const childProcess = spawn('npm', commandArgs, {
|
|
30
|
+
stdio: 'inherit',
|
|
31
|
+
cwd: './dist',
|
|
32
|
+
});
|
|
33
|
+
childProcess.on('close', code => {
|
|
34
|
+
if (code !== EXIT_CODES.SUCCESS) {
|
|
35
|
+
reject();
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
resolve();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
export async function updateNextTag(packageName, newVersion, otp, isDryRun, registry) {
|
|
44
|
+
logger.log();
|
|
45
|
+
logger.log(`Updating ${TAG.NEXT} tag...`);
|
|
46
|
+
const commandArgs = [
|
|
47
|
+
'dist-tag',
|
|
48
|
+
'add',
|
|
49
|
+
`${packageName}@${newVersion}`,
|
|
50
|
+
TAG.NEXT,
|
|
51
|
+
'--registry',
|
|
52
|
+
registry,
|
|
53
|
+
'--otp',
|
|
54
|
+
otp,
|
|
55
|
+
];
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
if (isDryRun) {
|
|
58
|
+
const distTagCommand = ['npm', ...commandArgs].join(' ');
|
|
59
|
+
logger.log(`Dry run: skipping run of \`${distTagCommand}\``);
|
|
60
|
+
resolve();
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const childProcess = spawn('npm', commandArgs, { stdio: 'inherit' });
|
|
64
|
+
childProcess.on('close', code => {
|
|
65
|
+
if (code !== EXIT_CODES.SUCCESS) {
|
|
66
|
+
reject();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
logger.success(`${TAG.NEXT} tag updated successfully`);
|
|
70
|
+
resolve();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|