Skip to content

Commit

Permalink
Merge pull request #173 from cosmology-tech/feat/new-cli
Browse files Browse the repository at this point in the history
upgrades, warn on outdated versions, install flag
  • Loading branch information
pyramation authored Apr 6, 2024
2 parents 18d757c + 256476a commit 2e25ea2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/create-cosmos-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"inquirerer": "0.1.3",
"minimist": "1.2.7",
"mkdirp": "1.0.4",
"semver": "^7.6.0",
"shelljs": "0.8.5"
}
}
7 changes: 4 additions & 3 deletions packages/create-cosmos-app/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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);
};
1 change: 1 addition & 0 deletions packages/create-cosmos-app/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CCA_URL = 'https://github.com/cosmology-tech/create-cosmos-app.git';
2 changes: 1 addition & 1 deletion packages/create-cosmos-app/src/create-cosmos-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();
93 changes: 69 additions & 24 deletions packages/create-cosmos-app/src/git-cca-template.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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([
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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(`
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 2e25ea2

Please sign in to comment.