|
| 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 | +} |
0 commit comments