Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Fix possible race condition during blitz new #198

Merged
merged 5 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/cli/src/generators/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import readDirRecursive from 'fs-readdir-recursive'
import spawn from 'cross-spawn'
import chalk from 'chalk'
import username from 'username'
import {readJSONSync} from 'fs-extra'
import {readJSONSync, writeJson} from 'fs-extra'
import {join} from 'path'
import {replaceDependencies} from '../utils/replace-dependencies'
import {replaceBlitzDependency} from '../utils/replace-blitz-dependency'
Expand Down Expand Up @@ -42,16 +42,23 @@ class AppGenerator extends Generator<AppGeneratorOptions> {
}

async postWrite() {
const pkg = readJSONSync(join(this.destinationPath(), 'package.json'))
const pkgJsonLocation = join(this.destinationPath(), 'package.json')
const pkg = readJSONSync(pkgJsonLocation)
const pkgDependencies = Object.keys(pkg.dependencies)
const pkgDevDependencies = Object.keys(pkg.devDependencies)

await Promise.all([
replaceDependencies(pkg, this.destinationPath(), pkgDependencies, 'dependencies'),
replaceDependencies(pkg, this.destinationPath(), pkgDevDependencies, 'devDependencies'),
replaceBlitzDependency(pkg, this.destinationPath(), this.options.version),
const dependenciesArray = await Promise.all([
replaceDependencies(pkg, pkgDependencies, 'dependencies'),
replaceDependencies(pkg, pkgDevDependencies, 'devDependencies'),
])

const dependencies = dependenciesArray.reduce((prev, {key, dependencies}) => {
prev[key] = replaceBlitzDependency(dependencies, this.options.version)
return prev
}, {} as Record<string, Record<string, string>>)
flybayer marked this conversation as resolved.
Show resolved Hide resolved

await writeJson(pkgJsonLocation, {...pkg, ...dependencies}, {spaces: 2})

console.log(chalk.hex(themeColor).bold('\nInstalling dependencies...'))
console.log('Scary warning messages during this part are unfortunately normal.\n')

Expand Down
12 changes: 5 additions & 7 deletions packages/cli/src/utils/replace-blitz-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {writeJSONSync} from 'fs-extra'
import {join} from 'path'
export const replaceBlitzDependency = (depends: Record<string, string>, cliVersion: string) => {
if (depends.blitz) {
depends.blitz = cliVersion.includes('canary') ? 'canary' : 'latest'
}

export const replaceBlitzDependency = async (pkg: any, desinationPath: string, cliVersion: string) => {
const newVersion = cliVersion.includes('canary') ? 'canary' : 'latest'

pkg.dependencies.blitz = newVersion
writeJSONSync(join(desinationPath, 'package.json'), pkg, {spaces: 2})
return depends
}
12 changes: 2 additions & 10 deletions packages/cli/src/utils/replace-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import {writeJSONSync} from 'fs-extra'
import {join} from 'path'
import {getLatestVersion} from '../utils/get-latest-version'

export const replaceDependencies = async (
pkg: any,
desinationPath: string,
dependencies: string[],
key: string,
) => {
export const replaceDependencies = async (pkg: any, dependencies: string[], key: string) => {
const latestVersions = await Promise.all(
dependencies.map(async (dependency) => {
const templateVersion = pkg[key][dependency]
Expand All @@ -19,6 +12,5 @@ export const replaceDependencies = async (
}),
)

pkg[key] = dependencies.reduce((o, k, i) => ({...o, [k]: latestVersions[i]}), {})
writeJSONSync(join(desinationPath, 'package.json'), pkg, {spaces: 2})
return {key, dependencies: dependencies.reduce((o, k, i) => ({...o, [k]: latestVersions[i]}), {})}
}