Skip to content

Commit

Permalink
Move create command into dedicated package (#31)
Browse files Browse the repository at this point in the history
* Move create into package

* Add create-bgdk to snapshot script

* copy example into create script on publish

* fix path to examples

* Create project with new package

* Add change file
  • Loading branch information
ehyland authored Dec 15, 2021
1 parent 1ccc244 commit a343c9c
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-waves-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'bgdk': patch
'create-bgdk': patch
---

Move create command to create-bgdk
9 changes: 3 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ jobs:
- name: publish
run: scripts/publish-snapshot-release

- run: echo "::set-output name=SCRATCH_PATH::$(mktemp -d)"
id: tmp-dir

- run: |
`pnpm bin`/jest tests/e2e/commands/create.test.ts
env:
SCRATCH_PATH: ${{ steps.tmp-dir.outputs.SCRATCH_PATH }}
SCRATCH_PATH: tests/e2e/workspace/test-1
- run: |
`pnpm bin`/jest tests/e2e/commands/build.test.ts
env:
SCRATCH_PATH: ${{ steps.tmp-dir.outputs.SCRATCH_PATH }}
SCRATCH_PATH: tests/e2e/workspace/test-1
- run: |
`pnpm bin`/jest tests/e2e/commands/dev.test.ts
env:
SCRATCH_PATH: ${{ steps.tmp-dir.outputs.SCRATCH_PATH }}
SCRATCH_PATH: tests/e2e/workspace/test-1
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Or, create react app for vanilla-extract apps.
## Create a new app

```bash
npx bgdk create ./my-new-app
# with npm
npm init bgdk ./my-new-app

# with yarn
yarn create bgdk ./my-new-app
```

## Start the app in dev mode
Expand Down
36 changes: 8 additions & 28 deletions packages/bgdk/src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
import path from 'path';
import fs from 'fs-extra';
import execa from 'execa';
import { log } from '../lib/log';
import chalk from 'chalk';

interface Options {
appPath: string;
}

export async function main(options: Options) {
let outputPath = path.resolve(options.appPath);
let outputPathRelative = path.relative(process.cwd(), outputPath);
let templatePath = path.resolve(__dirname, '../../../examples/basic');
log(`Command moved to create-bgdk package.`);
log(`To bootstrap project run:
log(`Creating app in ${outputPathRelative}`);
$ ${chalk.whiteBright(`npm init bgdk`)}
await fs.copy(templatePath, outputPath, { overwrite: true });
Or
// Fix .gitignore. See [issue](https://github.com/npm/npm/issues/1862).
try {
await fs.move(
path.resolve(outputPath, '.npmignore'),
path.resolve(outputPath, '.gitignore'),
{ overwrite: true },
);
} catch (error) {
log(`Failed creating gitignore, assuming this is local dev`);
}
$ ${chalk.whiteBright(`yarn create bgdk`)}
`);

log(`Installing deps`);

await execa.command('yarn install', {
stdio: 'inherit',
cwd: outputPath,
env: {
// Dev dependencies are skipped when NODE_ENV === 'production'
NODE_ENV: undefined,
},
});
process.exit(1);
}
5 changes: 5 additions & 0 deletions packages/create-bgdk/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

'use strict';
require('v8-compile-cache');
require('./cli');
3 changes: 3 additions & 0 deletions packages/create-bgdk/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "dist/create-bgdk-cli.cjs.js"
}
1 change: 1 addition & 0 deletions packages/create-bgdk/examples
45 changes: 45 additions & 0 deletions packages/create-bgdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "create-bgdk",
"version": "0.0.0",
"description": "Bootstrap apps with BGDK",
"main": "dist/create-bgdk.cjs.js",
"files": [
"bin.js",
"/cli",
"/dist",
"/examples",
"/examples/basic/.gitignore",
"!/examples/*/yarn.yaml",
"/templates/"
],
"preconstruct": {
"entrypoints": [
"cli/index.ts"
]
},
"bin": "./bin.js",
"publishConfig": {
"access": "public"
},
"keywords": [
"vanilla-extract",
"bgdk"
],
"author": "Eamon Hyland",
"license": "MIT",
"bugs": {
"url": "https://github.com/ehyland/bgdk/issues"
},
"homepage": "https://github.com/ehyland/bgdk#readme",
"repository": {
"type": "git",
"url": "git@github.com:ehyland/bgdk.git",
"directory": "packages/create-bgdk"
},
"dependencies": {
"chalk": "^4.1.2",
"fs-extra": "^10.0.0",
"sade": "^1.7.4",
"v8-compile-cache": "^2.3.0"
}
}
28 changes: 28 additions & 0 deletions packages/create-bgdk/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sade from 'sade';
import { main } from '../create';

const prog = sade('create-bgdk <app-path>', true);

// exit on unhandled promise
process.on('unhandledRejection', (err) => {
throw err;
});

// exit on signals
['SIGINT', 'SIGTERM'].forEach(function (sig) {
process.on(sig, function () {
process.exit();
});
});

const pkg = require('../../package.json');

prog.version(pkg.version);

prog
.describe('Create a new project with bgdk')
.action(async (appPath: string) => {
await main({ appPath });
});

prog.parse(process.argv);
50 changes: 50 additions & 0 deletions packages/create-bgdk/src/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import path from 'path';
import fs from 'fs-extra';
import { log } from './log';
import chalk from 'chalk';

interface Options {
appPath: string;
}

export async function main(options: Options) {
let outputPath = path.resolve(options.appPath);
let outputPathRelative = path.relative(process.cwd(), outputPath);
let templatePath = path.resolve(__dirname, '../../examples/basic');

log(`Creating app in ${outputPathRelative}`);

if (await fs.pathExists(outputPath)) {
const { isDirectory } = await fs.stat(outputPath);

if (!isDirectory) {
log(`File exists at output path ${outputPathRelative}`);
process.exit(1);
}

const files = await fs.readdir(outputPath);

if (files.length !== 0) {
log(`${outputPathRelative} is not empty`);
process.exit(1);
}
}

await fs.copy(templatePath, outputPath, { overwrite: true });

// Fix .gitignore. See [issue](https://github.com/npm/npm/issues/1862).
try {
await fs.move(
path.resolve(outputPath, '.npmignore'),
path.resolve(outputPath, '.gitignore'),
{ overwrite: true },
);
} catch (error) {
// assuming this is local dev
}

log(`App created! Next steps
$ ${chalk.whiteBright(`cd ${outputPathRelative}`)}
$ ${chalk.whiteBright(`yarn install`)}
`);
}
5 changes: 5 additions & 0 deletions packages/create-bgdk/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import chalk from 'chalk';

export const log = (message: string) => {
console.log(`🥦 ${chalk.blue(message)}`);
};
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions scripts/prepare-for-publish
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ if [ -e "./packages/bgdk/examples" ]; then
rm ./packages/bgdk/examples
fi

if [ -e "./packages/create-bgdk/examples" ]; then
rm ./packages/create-bgdk/examples
fi

cp -r examples ./packages/bgdk/
cp -r examples ./packages/create-bgdk/

# build the packages
pnpm build
1 change: 1 addition & 0 deletions scripts/publish-snapshot-release
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -euxo pipefail
# manually create the changeset
echo "---
'bgdk': patch
'create-bgdk': patch
'@bgdk/storybook': patch
'bgdk-basic-example': patch
---
Expand Down
70 changes: 53 additions & 17 deletions tests/e2e/commands/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@ import { ResolveType, clearScratchSpace, SCRATCH_PATH } from '../test-utils';

jest.setTimeout(ms('5 minutes'));

async function cmd(
cmdString: string,
options?: Omit<execa.Options, 'all' | 'encoding' | 'extendEnv' | 'env'>,
) {
console.log(`Running ${cmdString}`);

const child = execa.command(cmdString, {
all: true,
encoding: 'utf8',
extendEnv: false,
env: {
PATH: process.env.PATH,
NPM_CONFIG_USERCONFIG: process.env.NPM_CONFIG_USERCONFIG,
NODE_AUTH_TOKEN: process.env.NODE_AUTH_TOKEN,
SCRATCH_PATH: process.env.SCRATCH_PATH,
},
...options,
});

child.all?.pipe(process.stderr);

return child;
}

describe('bgdk create app', () => {
let ctx: ResolveType<typeof setup>;

const setup = async () => {
await clearScratchSpace();

console.log(`Running npx bgdk@dev create ${SCRATCH_PATH}`);

const child = execa.command(`npx bgdk@dev create ${SCRATCH_PATH}`, {
all: true,
encoding: 'utf8',
extendEnv: false,
env: {
PATH: process.env.PATH,
NPM_CONFIG_USERCONFIG: process.env.NPM_CONFIG_USERCONFIG,
NODE_AUTH_TOKEN: process.env.NODE_AUTH_TOKEN,
SCRATCH_PATH: process.env.SCRATCH_PATH,
},
});

child.all?.pipe(process.stderr);

return { result: await child };
return { result: await cmd(`npm init bgdk@dev ${SCRATCH_PATH}`) };
};

beforeAll(async () => {
Expand All @@ -45,4 +53,32 @@ describe('bgdk create app', () => {
const packageStat = await fs.stat(packagePath);
expect(packageStat.isFile()).toBe(true);
});

describe('yarn install', () => {
beforeAll(async () => {
await cmd(`yarn install`, {
cwd: SCRATCH_PATH,
});
});

it('installs successfully', async () => {
expect((await fs.readdir(SCRATCH_PATH)).sort()).toMatchInlineSnapshot(`
Array [
".babelrc",
".browserslistrc",
".gitignore",
".prettierrc",
".storybook",
"CHANGELOG.md",
"README.md",
"jest.config.js",
"node_modules",
"package.json",
"src",
"tsconfig.json",
"yarn.lock",
]
`);
});
});
});
1 change: 1 addition & 0 deletions tests/e2e/workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*/*

0 comments on commit a343c9c

Please sign in to comment.