From fc2b7f747bf15b8c9329f2940ed2f6cf71353c33 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 19 Apr 2018 14:10:48 +0900 Subject: Add checks for the required local setup before running the publish script --- packages/monorepo-scripts/src/publish.ts | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index a2d641ff9..9bd9ff6be 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -38,6 +38,11 @@ const packageNameToWebsitePath: { [name: string]: string } = { }; (async () => { + const hasRequiredSetup = await checkPublishRequiredSetupAsync(); + if (!hasRequiredSetup) { + return; // abort + } + // Fetch public, updated Lerna packages const updatedPublicLernaPackages = await getUpdatedPublicLernaPackagesAsync(); @@ -107,6 +112,51 @@ package.ts. Please add an entry for it and try again.`, } } +async function checkPublishRequiredSetupAsync(): Promise { + // check to see if logged into npm before publishing (npm whoami) + try { + await execAsync(`sudo npm whoami`); + } catch (err) { + utils.log('You must be logged into npm in the commandline to publish. Run `npm login` and try again.'); + return false; + } + + // Check to see if Git creds setup (check for ENV 'GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS') + if (_.isUndefined(process.env.GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS)) { + utils.log( + 'You must have a Github personal access token set to an envVar named `GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS`. Add it then try again.', + ); + return false; + } + // Check NPM version is 5.X + const result = await execAsync(`npm --version`); + const version = result.stdout; + const versionSegments = version.split('.'); + const majorVersion = _.parseInt(versionSegments[0]); + if (majorVersion < 5) { + utils.log('You npm version must be v5.x or higher. Upgrade your npm and try again.'); + return false; + } + + // Check that `aws` commandline is installed + try { + await execAsync(`aws help`); + } catch (err) { + utils.log('You must have `awscli` commandline tool installed. Install it and try again.'); + return false; + } + + // Check that `aws` creds are setup + try { + await execAsync(`aws sts get-caller-identity`); + } catch (err) { + utils.log('You must setup your AWS credentials by running `aws configure`. Do this and try again.'); + return false; + } + + return true; +} + async function pushChangelogsToGithubAsync() { await execAsync(`git add . --all`, { cwd: constants.monorepoRootPath }); await execAsync(`git commit -m "Updated CHANGELOGS"`, { cwd: constants.monorepoRootPath }); -- cgit v1.2.3 From 858d1768cecf165adefcdf860f247dad258d8cae Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 19 Apr 2018 14:14:10 +0900 Subject: Improve comments --- packages/monorepo-scripts/src/publish.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 9bd9ff6be..827d6165e 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -113,7 +113,7 @@ package.ts. Please add an entry for it and try again.`, } async function checkPublishRequiredSetupAsync(): Promise { - // check to see if logged into npm before publishing (npm whoami) + // check to see if logged into npm before publishing try { await execAsync(`sudo npm whoami`); } catch (err) { @@ -121,13 +121,14 @@ async function checkPublishRequiredSetupAsync(): Promise { return false; } - // Check to see if Git creds setup (check for ENV 'GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS') + // Check to see if Git personal token setup if (_.isUndefined(process.env.GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS)) { utils.log( 'You must have a Github personal access token set to an envVar named `GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS`. Add it then try again.', ); return false; } + // Check NPM version is 5.X const result = await execAsync(`npm --version`); const version = result.stdout; @@ -138,7 +139,7 @@ async function checkPublishRequiredSetupAsync(): Promise { return false; } - // Check that `aws` commandline is installed + // Check that `aws` commandline tool is installed try { await execAsync(`aws help`); } catch (err) { @@ -146,7 +147,7 @@ async function checkPublishRequiredSetupAsync(): Promise { return false; } - // Check that `aws` creds are setup + // Check that `aws` credentials are setup try { await execAsync(`aws sts get-caller-identity`); } catch (err) { -- cgit v1.2.3 From 2b15c03b9ac516f7e38eef174503096026162b90 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 19 Apr 2018 14:19:34 +0900 Subject: Use methods moved to utils since now shared by multiple scripts --- packages/monorepo-scripts/src/publish.ts | 36 +++----------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 827d6165e..2ae9bbb60 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -44,7 +44,8 @@ const packageNameToWebsitePath: { [name: string]: string } = { } // Fetch public, updated Lerna packages - const updatedPublicLernaPackages = await getUpdatedPublicLernaPackagesAsync(); + const shouldIncludePrivate = false; + const updatedPublicLernaPackages = await utils.getUpdatedLernaPackagesAsync(shouldIncludePrivate); await confirmDocPagesRenderAsync(updatedPublicLernaPackages); @@ -165,23 +166,12 @@ async function pushChangelogsToGithubAsync() { utils.log(`Pushed CHANGELOG updates to Github`); } -async function getUpdatedPublicLernaPackagesAsync(): Promise { - const updatedPublicPackages = await getPublicLernaUpdatedPackagesAsync(); - const updatedPackageNames = _.map(updatedPublicPackages, pkg => pkg.name); - - const allLernaPackages = lernaGetPackages(constants.monorepoRootPath); - const updatedPublicLernaPackages = _.filter(allLernaPackages, pkg => { - return _.includes(updatedPackageNames, pkg.package.name); - }); - return updatedPublicLernaPackages; -} - async function updateChangeLogsAsync(updatedPublicLernaPackages: LernaPackage[]): Promise { const packageToVersionChange: PackageToVersionChange = {}; for (const lernaPackage of updatedPublicLernaPackages) { const packageName = lernaPackage.package.name; const changelogJSONPath = path.join(lernaPackage.location, 'CHANGELOG.json'); - const changelogJSON = getChangelogJSONOrCreateIfMissing(lernaPackage.package.name, changelogJSONPath); + const changelogJSON = utils.getChangelogJSONOrCreateIfMissing(changelogJSONPath); let changelogs: Changelog[]; try { changelogs = JSON.parse(changelogJSON); @@ -276,13 +266,6 @@ async function lernaPublishAsync(packageToVersionChange: { [name: string]: strin }); } -async function getPublicLernaUpdatedPackagesAsync(): Promise { - const result = await execAsync(`${LERNA_EXECUTABLE} updated --json`, { cwd: constants.monorepoRootPath }); - const updatedPackages = JSON.parse(result.stdout); - const updatedPublicPackages = _.filter(updatedPackages, updatedPackage => !updatedPackage.private); - return updatedPublicPackages; -} - function updateVersionNumberIfNeeded(currentVersion: string, proposedNextVersion: string) { if (proposedNextVersion === currentVersion) { return utils.getNextPatchVersion(currentVersion); @@ -294,19 +277,6 @@ function updateVersionNumberIfNeeded(currentVersion: string, proposedNextVersion return proposedNextVersion; } -function getChangelogJSONOrCreateIfMissing(packageName: string, changelogPath: string): string { - let changelogJSON: string; - try { - changelogJSON = fs.readFileSync(changelogPath, 'utf-8'); - return changelogJSON; - } catch (err) { - // If none exists, create new, empty one. - const emptyChangelogJSON = JSON.stringify([], null, 4); - fs.writeFileSync(changelogPath, emptyChangelogJSON); - return emptyChangelogJSON; - } -} - function shouldAddNewChangelogEntry(currentVersion: string, changelogs: Changelog[]): boolean { if (_.isEmpty(changelogs)) { return true; -- cgit v1.2.3 From 417cec9e04af84935c38ed77cdb53a8026b0004e Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 20 Apr 2018 09:36:03 +0900 Subject: Consolidate github personal access token env to one place: constants.ts --- packages/monorepo-scripts/src/publish.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 2ae9bbb60..6c728467a 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -123,7 +123,7 @@ async function checkPublishRequiredSetupAsync(): Promise { } // Check to see if Git personal token setup - if (_.isUndefined(process.env.GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS)) { + if (_.isUndefined(constants.githubPersonalAccessToken)) { utils.log( 'You must have a Github personal access token set to an envVar named `GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS`. Add it then try again.', ); -- cgit v1.2.3 From a3cc5c1dd7e88aea50859560f7d751435cb577bd Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 24 Apr 2018 10:32:44 +0900 Subject: Add hack comment about the use of sudo --- packages/monorepo-scripts/src/publish.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'packages/monorepo-scripts/src/publish.ts') diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 6c728467a..0ca3ea7a1 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -116,6 +116,8 @@ package.ts. Please add an entry for it and try again.`, async function checkPublishRequiredSetupAsync(): Promise { // check to see if logged into npm before publishing try { + // HACK: for some reason on some setups, the `npm whoami` will not recognize a logged-in user + // unless run with `sudo` (i.e Fabio's NVM setup) but is fine for others (Jacob's N setup). await execAsync(`sudo npm whoami`); } catch (err) { utils.log('You must be logged into npm in the commandline to publish. Run `npm login` and try again.'); -- cgit v1.2.3