diff --git a/packages/create-cosmos-app/package.json b/packages/create-cosmos-app/package.json index f76344a19..eb617d7aa 100644 --- a/packages/create-cosmos-app/package.json +++ b/packages/create-cosmos-app/package.json @@ -65,6 +65,7 @@ "inquirerer": "0.1.3", "minimist": "1.2.7", "mkdirp": "1.0.4", + "semver": "^7.6.0", "shelljs": "0.8.5" } } diff --git a/packages/create-cosmos-app/src/cli.ts b/packages/create-cosmos-app/src/cli.ts index e7cc6d646..4c376c585 100644 --- a/packages/create-cosmos-app/src/cli.ts +++ b/packages/create-cosmos-app/src/cli.ts @@ -1,6 +1,7 @@ +import { CCA_URL } from "./constants"; import { createGitApp } from "./git-cca-template"; -export const cli = async (argv) => { - const url = argv.repo ?? 'https://github.com/cosmology-tech/create-cosmos-app.git'; - const createCosmosApp = createGitApp(url); +export const cli = async (argv, version) => { + const repo = argv.repo ?? CCA_URL; + const createCosmosApp = createGitApp(repo, version); await createCosmosApp(argv); }; \ No newline at end of file diff --git a/packages/create-cosmos-app/src/constants.ts b/packages/create-cosmos-app/src/constants.ts new file mode 100644 index 000000000..4b1fe7731 --- /dev/null +++ b/packages/create-cosmos-app/src/constants.ts @@ -0,0 +1 @@ +export const CCA_URL = 'https://github.com/cosmology-tech/create-cosmos-app.git'; \ No newline at end of file diff --git a/packages/create-cosmos-app/src/create-cosmos-app.ts b/packages/create-cosmos-app/src/create-cosmos-app.ts index 2eff81e23..0aefc200f 100755 --- a/packages/create-cosmos-app/src/create-cosmos-app.ts +++ b/packages/create-cosmos-app/src/create-cosmos-app.ts @@ -16,6 +16,6 @@ var argv = require('minimist')(process.argv.slice(2)); ) { shell.exec(`npm install -g create-cosmos-app@latest`); } else { - await cli(argv); + await cli(argv, pkg.version); } })(); diff --git a/packages/create-cosmos-app/src/git-cca-template.ts b/packages/create-cosmos-app/src/git-cca-template.ts index 83d5f3a01..f07aea133 100644 --- a/packages/create-cosmos-app/src/git-cca-template.ts +++ b/packages/create-cosmos-app/src/git-cca-template.ts @@ -1,22 +1,29 @@ import * as shell from 'shelljs'; +import semver from 'semver'; import * as c from 'ansi-colors'; import { prompt } from './prompt'; -import { join, dirname, basename } from 'path'; +import { join, dirname, basename, sep, relative } from 'path'; import { sync as mkdirp } from 'mkdirp'; import { sync as glob } from 'glob'; import * as fs from 'fs'; import { cloneRepo, getPackageLicAndAccessInfo, getQuestionsAndAnswers, getTemplateFolder } from './utils'; +import { CCA_URL } from './constants'; +const requiredTools = ['git', 'yarn']; -export const createGitApp = (repo: string) => { +export const createGitApp = (repo: string, version: string) => { return async argv => { - if (!shell.which('git')) { - shell.echo('Sorry, this script requires git'); - return shell.exit(1); - } - if (!shell.which('yarn')) { - shell.echo('Sorry, this script requires yarn'); - return shell.exit(1); + + // if --no-install is set, don't touch! + if (!argv.hasOwnProperty('install')) argv.install = true; + + // check required tools + for (const tool of requiredTools) { + if (!shell.which(tool)) { + shell.echo(`Sorry, this script requires ${tool}.`); + return shell.exit(1); + } } + let { name } = await prompt([ { name: 'name', @@ -27,13 +34,21 @@ export const createGitApp = (repo: string) => { name = name.replace(/\s/g, '-'); const folderName = await getTemplateFolder(argv); - const currentDirectory = process.cwd(); - const dir = cloneRepo(argv, repo, name); - + const tempDir = cloneRepo(argv, repo, name); + const clonedRepoDir = join(tempDir, name); // cd into the cloned repo from $dir shell.cd(name); + // warn about upgrades + if (repo === CCA_URL) { + const rootPkgPath = join(clonedRepoDir, 'packages/create-cosmos-app/package.json'); + const rootPkg = JSON.parse(fs.readFileSync(rootPkgPath, 'utf-8')); + if (semver.gt(rootPkg.version, version)) { + console.warn(c.yellow(`⚠️ You are using create-cosmos-app version ${c.red(rootPkg.version)}, but version ${c.green(version)} is available. Run "${c.cyan('cca upgrade')}" or "${c.cyan('npm install -g create-cosmos-app@latest')}" to upgrade.`)); + } + } + // get template const list = shell.ls(`./${folderName}`); const { template } = await prompt([ @@ -51,6 +66,7 @@ export const createGitApp = (repo: string) => { const hasResults = Object.keys(results).length > 0; + // KEEP THIS CODE FOR BOILERPLATES! let license = {}; let scopedResults = {}; if (hasResults) { @@ -75,6 +91,7 @@ export const createGitApp = (repo: string) => { // LICENSE if ( basename(templateFile) === 'LICENSE' && + // @ts-ignore license.__LICENSE__ === 'closed' ) { content = `Copyright (c) ${new Date().getFullYear()} __USERFULLNAME__ <__USEREMAIL__> - All Rights Reserved @@ -92,6 +109,7 @@ export const createGitApp = (repo: string) => { // access if (hasResults) { if (results.__ACCESS__ === 'public') { + // @ts-ignore if (scopedResults.scoped) { content = content.replace( /__PACKAGE_IDENTIFIER__/g, @@ -124,24 +142,51 @@ export const createGitApp = (repo: string) => { } shell.cd(currentDirectory); - shell.rm('-rf', dir); + shell.rm('-rf', tempDir); shell.cd(`./${name}`); - // clean up lock-file business... - const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8')); - delete pkg.scripts['locks:remove'] - delete pkg.scripts['locks:create'] - delete pkg.scripts['locks'] - delete pkg.devDependencies['generate-lockfile'] - fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2)); + const pkgJsons = [] + .concat(glob(join(currentDirectory, name, '/**/package.json'))); + + let pkgJsonFound = true; + let closestPkgJson = null; + if (pkgJsons.length === 0) { + console.log('No package.json file found'); + pkgJsonFound = false; + } else { + // Find the shortest path + closestPkgJson = pkgJsons.reduce((shortest, current) => { + return current.split(sep).length < shortest.split(sep).length ? current : shortest; + }); + } + - // now yarn... - shell.exec(`yarn`); + let rel; + if (pkgJsonFound) { + // clean up lock-file business... + const pkg = JSON.parse(fs.readFileSync(closestPkgJson, 'utf-8')); + delete pkg.scripts['locks:remove'] + delete pkg.scripts['locks:create'] + delete pkg.scripts['locks'] + delete pkg.devDependencies['generate-lockfile'] + fs.writeFileSync(closestPkgJson, JSON.stringify(pkg, null, 2)); + + // now yarn... + if (argv.install) { + shell.cd(dirname(closestPkgJson)); + shell.exec(`yarn`); + } + + rel = relative(currentDirectory, dirname(closestPkgJson)); + } shell.cd(currentDirectory); + let cmd = `cd ./${name}`; - if (!hasResults) { - cmd += ' && yarn dev'; + if (!hasResults && pkgJsonFound) { + // if rel, then set manually + cmd = `cd ./${rel}`; + cmd += `\nyarn dev` } console.log(` diff --git a/yarn.lock b/yarn.lock index 75f009576..d304aa3ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19345,7 +19345,7 @@ semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0, semver@^6.3.1: resolved "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4" integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA== -semver@^7.1.1, semver@^7.1.3, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.5.0, semver@^7.5.3, semver@^7.5.4: +semver@^7.1.1, semver@^7.1.3, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.5.0, semver@^7.5.3, semver@^7.5.4, semver@^7.6.0: version "7.6.0" resolved "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz#1a46a4db4bffcccd97b743b5005c8325f23d4e2d" integrity sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==