Skip to content

Commit

Permalink
add init command, remove no command behavior (#1526)
Browse files Browse the repository at this point in the history
* add init command, remove no command behavior

* lint fix

* test fix
  • Loading branch information
FredKSchott authored Nov 6, 2020
1 parent 14c10ec commit d20c655
Show file tree
Hide file tree
Showing 45 changed files with 82 additions and 43 deletions.
11 changes: 11 additions & 0 deletions snowpack/assets/snowpack-init-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Snowpack Configuration File
// See all supported options: https://www.snowpack.dev/#configuration

/** @type {import("snowpack").SnowpackUserConfig } */
module.exports = {
// mount: {},
// plugins: [],
// installOptions: {},
// devOptions: {},
// buildOptions: {},
};
18 changes: 18 additions & 0 deletions snowpack/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {promises as fs, existsSync, constants as fsConstants} from 'fs';
import {bold, dim} from 'kleur/colors';
import path from 'path';
import {logger} from '../logger';
import {CommandOptions} from '../types/snowpack';

export async function command(commandOptions: CommandOptions) {
const {cwd} = commandOptions;
logger.info(`Creating new project configuration file... ${dim('(snowpack.config.js)')}`);
const templateLoc = path.join(__dirname, '../../assets/snowpack-init-file.js');
const destLoc = path.join(cwd, 'snowpack.config.js');
if (existsSync(destLoc)) {
logger.error(`Error: File already exists, cannot overwrite ${destLoc}`);
process.exit(1);
}
await fs.copyFile(templateLoc, destLoc, fsConstants.COPYFILE_EXCL);
logger.info(`File created! Open ${bold('snowpack.config.js')} to customize your project.`);
}
12 changes: 11 additions & 1 deletion snowpack/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import util from 'util';
import yargs from 'yargs-parser';
import {addCommand, rmCommand} from './commands/add-rm';
import {command as initCommand} from './commands/init';
import {command as buildCommand} from './commands/build';
import {command as devCommand} from './commands/dev';
import {command as installCommand} from './commands/install';
Expand Down Expand Up @@ -44,6 +45,7 @@ ${colors.bold(`snowpack`)} - A faster build system for the modern web.
📖 ${colors.dim('https://www.snowpack.dev/#configuration')}
${colors.bold('Commands:')}
snowpack init Create a new project config file.
snowpack dev Develop your app locally.
snowpack build Build your app for production.
snowpack install (Advanced) Install web-ready dependencies.
Expand Down Expand Up @@ -92,8 +94,12 @@ export async function cli(args: string[]) {
process.exit(1);
}

const cmd = cliFlags['_'][2] || 'install';
const cmd = cliFlags['_'][2];
logger.debug(`run command: ${cmd}`);
if (!cmd) {
printHelp();
process.exit(1);
}

// Set this early -- before config loading -- so that plugins see it.
if (cmd === 'build') {
Expand Down Expand Up @@ -129,6 +135,10 @@ export async function cli(args: string[]) {
process.exit(1);
}

if (cmd === 'init') {
await initCommand(commandOptions);
return process.exit(0);
}
if (cmd === 'build') {
await buildCommand(commandOptions);
return process.exit(0);
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/auto-named-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-auto-named-exports",
"description": "Handle automatic named export detection for cjs packages",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-alias",
"description": "Handle deep imports to package entrypoints",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"alias": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-custom-path/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-config-custom-path",
"scripts": {
"testinstall": "snowpack --config snowpack.dev.json"
"testinstall": "snowpack install --config snowpack.dev.json"
},
"dependencies": {
"array-flatten": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-extends/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-config-extends",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"dependencies": {
"array-flatten": "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-external/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-external",
"description": "Handle external packages",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"installOptions": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-named-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-named-exports",
"description": "Handle installOptions.namedExports",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-package-lookup-fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-package-lookup-fields",
"description": "Handle custom packageLookupFields",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"installOptions": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-polyfill-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-config-polyfill-node",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-config-rollup",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"dependencies": {
"rollup-plugin-svelte": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/config-with-cli-flags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-with-cli-flags",
"description": "Test that the --optimize flag overrides the config",
"scripts": {
"testinstall": "snowpack --no-source-map"
"testinstall": "snowpack install --no-source-map"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-browser-entrypoint-object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-dep-browser-entrypoint-object",
"description": "Test that we properly handle a package.json 'browser' object.",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-dep-exports",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-list-complex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-dep-list-complex",
"description": "Test packages with tricky names",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-list-css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-dep-list-css",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"dependencies": {
"css-mock-pkg-a": "file:./packages/css-mock-pkg-a",
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-list-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-dep-list-simple",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-node-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-dep-node-fetch",
"description": "Handle specific node.js fetch polyfill packages",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-remote-url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-dep-remote-url",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/dep-types-only/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-dep-types-only",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-config-invalid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-config-invalid",
"description": "Test invalid configuration (install should be array)",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": "shallow-equal"
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-empty-dep-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-empty-dep-list",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": []
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-file-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-file-ext",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-illegal-env-var/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-illegal-env-var",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"installOptions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-include-ignore-unsupported-files",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"devDependencies": {
"snowpack": "^2.14.3"
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-missing-dep/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-missing-dep",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-missing-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-missing-exports",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-no-dep-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-no-dep-list",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-node-builtin-unresolved/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-error-node-builtin-unresolved",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"exclude": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-react-source-pika/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-error-react-source-pika",
"description": "Test that error is reported when React workaround package is used.",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"webDependencies": {
"async": "~3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/error-react-workaround/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-error-react-workaround",
"description": "Test that error is reported when React workaround package is used.",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/exclude-external-packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-exclude-external-packages",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/exclude/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-exclude",
"scripts": {
"testinstall": "snowpack --exclude 'src/exclude.js'"
"testinstall": "snowpack install --exclude 'src/exclude.js'"
},
"snowpack": {
"buildOptions": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/export-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-export-maps",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"install": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-include-missing-in-package-json",
"scripts": {
"testinstall": "node copyPackages && snowpack"
"testinstall": "node copyPackages && snowpack install"
},
"snowpack": {
"mount": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/include-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-include-ts",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"buildOptions": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/include-with-alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.1",
"name": "@snowpack/test-include-with-alias",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"buildOptions": {
Expand Down
2 changes: 1 addition & 1 deletion test/esinstall/include-with-package-name/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "@snowpack/test-include-with-package-name",
"description": "Test that 'include' import scanner supports package name imports",
"scripts": {
"testinstall": "snowpack"
"testinstall": "snowpack install"
},
"snowpack": {
"buildOptions": {
Expand Down
Loading

1 comment on commit d20c655

@vercel
Copy link

@vercel vercel bot commented on d20c655 Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.