Skip to content

Commit 2d6032b

Browse files
committed
feat: sketch-cli add build
1 parent 6777104 commit 2d6032b

File tree

13 files changed

+1078
-317
lines changed

13 files changed

+1078
-317
lines changed

packages/cli/bin/sketch-build.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const chalk = require("chalk");
2+
const program = require("commander");
3+
const microbundle = require("microbundle");
4+
5+
const stdout = console.log.bind(console);
6+
const stderr = console.error.bind(console);
7+
8+
function logError(err) {
9+
const error = err.error || err;
10+
const description = `${error.name ? error.name + ": " : ""}${error.message || error}`;
11+
const message = error.plugin ? `(${error.plugin} plugin) ${description}` : description;
12+
13+
stderr(chalk.bold.red(message));
14+
15+
if (error.loc) {
16+
stderr();
17+
stderr(`at ${error.loc.file}:${error.loc.line}:${error.loc.column}`);
18+
}
19+
20+
if (error.frame) {
21+
stderr();
22+
stderr(chalk.dim(error.frame));
23+
} else if (err.stack) {
24+
const headlessStack = error.stack.replace(message, "");
25+
stderr(chalk.dim(headlessStack));
26+
}
27+
28+
stderr();
29+
}
30+
31+
program
32+
.option("-u, --umd <name>", "Specify name exposed in UMD builds")
33+
.option("-o, --output <folder>", "Directory to place build files into")
34+
.parse(process.argv);
35+
36+
const type = program.args[0] || "module";
37+
38+
const opts = {
39+
name: program.umd,
40+
output: program.output,
41+
format: "es,cjs,umd",
42+
cwd: ".",
43+
target: "node",
44+
compress: true,
45+
sourcemap: true,
46+
raw: false,
47+
};
48+
49+
if (type === "module") {
50+
microbundle(opts)
51+
.then(output => {
52+
if (output != null) stdout(output);
53+
if (!opts.watch) process.exit(0);
54+
})
55+
.catch(err => {
56+
process.exitCode = (typeof err.code === "number" && err.code) || 1;
57+
logError(err);
58+
process.exit();
59+
});
60+
}

packages/cli/bin/sketch-init.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const program = require("commander");
22
const inquirer = require("inquirer");
33
const path = require("path");
4+
const parseGitConfig = require("parse-git-config");
5+
const gitConfigPath = require("git-config-path");
46

57
const cli = require("../dist");
6-
const { gitUser } = require("../dist/lib");
8+
9+
function gitUser() {
10+
const gitConfig = parseGitConfig.sync({ cwd: "/", path: gitConfigPath("global") });
11+
return Object.assign({ name: "", email: "" }, gitConfig.user);
12+
}
713

814
program
915
.option("-si, --skip-install", "skip installation")

packages/cli/bin/sketch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var program = require("commander");
55

66
program
77
.version(pkg.version)
8+
.command("build [type]", "build a module, service or web app")
89
.command("init [dest]", "init a package in dest dir")
910
.command("update [dest]", "update a package in dest dir")
1011
.parse(process.argv);

packages/cli/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"url": "https://github.com/36node/sketch/issues"
1212
},
1313
"main": "dist/index.js",
14+
"umd:main": "dist/index.umd.js",
15+
"module": "dist/index.mjs",
1416
"files": [
1517
"bin",
1618
"dist",
@@ -20,7 +22,8 @@
2022
"sketch": "bin/sketch.js"
2123
},
2224
"scripts": {
23-
"build": "rm -rf dist && babel ./src -d ./dist --ignore test.js"
25+
"build": "microbundle",
26+
"test": "jest --silent --env=node"
2427
},
2528
"dependencies": {
2629
"commander": "^2.16.0",
@@ -29,6 +32,7 @@
2932
"git-config-path": "^1.0.1",
3033
"inquirer": "^6.0.0",
3134
"jsonfile": "^4.0.0",
35+
"microbundle": "^0.6.0",
3236
"npm-package-arg": "^6.1.0",
3337
"ora": "^3.0.0",
3438
"parse-git-config": "^2.0.2",

packages/cli/src/index.js

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,3 @@
1-
import path from "path";
2-
import ora from "ora";
3-
import { copy, remove } from "fs-extra";
1+
import init from "./init";
42

5-
import download from "./download-npm-package";
6-
import { jsonfile } from "./lib";
7-
8-
export async function init(tpl, dest = ".", options = {}) {
9-
const pkg = `@36node/template-${tpl}`;
10-
const spinner = ora(`Downloading template ${pkg}...`);
11-
12-
try {
13-
spinner.start();
14-
await download(pkg, dest);
15-
spinner.succeed("Downloading success.");
16-
} catch (err) {
17-
spinner.fail("Downloading failed.");
18-
throw err;
19-
}
20-
21-
// generate common template files
22-
try {
23-
spinner.text = "Generating common template files ...";
24-
spinner.start();
25-
await copy(path.join(__dirname, "../template"), dest, { overwrite: false });
26-
await remove(path.join(dest, "CHANGELOG.md"));
27-
spinner.succeed("Generating basic files success!");
28-
} catch (err) {
29-
spinner.fail("Generating basic files failed!");
30-
throw err;
31-
}
32-
33-
try {
34-
spinner.text = "Modifying package.json ...";
35-
spinner.start();
36-
const pkgFile = path.join(dest, "package.json");
37-
const pkgJson = await jsonfile.readFile(pkgFile);
38-
39-
let { name, owner, scope } = options;
40-
const files = ["bin", "dist"];
41-
const version = "0.0.0";
42-
const repository = {
43-
url: `${owner}/${name}`,
44-
type: "git",
45-
};
46-
name = scope ? `@${scope}/${name}` : name;
47-
await jsonfile.writeFile(
48-
pkgFile,
49-
{ ...pkgJson, name, repository, files, version },
50-
{ spaces: 2 }
51-
);
52-
spinner.succeed(`Package.json cooked! ${path.resolve(dest)}`);
53-
} catch (err) {
54-
spinner.fail("Modifying package.json failed.");
55-
throw err;
56-
}
57-
}
3+
export { init };

packages/cli/src/init.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import path from "path";
2+
import ora from "ora";
3+
import { copy, remove } from "fs-extra";
4+
5+
import download from "./download-npm-package";
6+
import { jsonfile } from "./lib";
7+
8+
export default async function init(tpl, dest = ".", options = {}) {
9+
const pkg = `@36node/template-${tpl}`;
10+
const spinner = ora(`Downloading template ${pkg}...`);
11+
12+
try {
13+
spinner.start();
14+
await download(pkg, dest);
15+
spinner.succeed("Downloading success.");
16+
} catch (err) {
17+
spinner.fail("Downloading failed.");
18+
throw err;
19+
}
20+
21+
// generate common template files
22+
try {
23+
spinner.text = "Generating common template files ...";
24+
spinner.start();
25+
await copy(path.join(__dirname, "../template"), dest, { overwrite: false });
26+
await remove(path.join(dest, "CHANGELOG.md"));
27+
spinner.succeed("Generating basic files success!");
28+
} catch (err) {
29+
spinner.fail("Generating basic files failed!");
30+
throw err;
31+
}
32+
33+
try {
34+
spinner.text = "Modifying package.json ...";
35+
spinner.start();
36+
const pkgFile = path.join(dest, "package.json");
37+
const pkgJson = await jsonfile.readFile(pkgFile);
38+
39+
let { name, owner, scope } = options;
40+
const files = ["bin", "dist"];
41+
const version = "0.0.0";
42+
const repository = {
43+
url: `${owner}/${name}`,
44+
type: "git",
45+
};
46+
name = scope ? `@${scope}/${name}` : name;
47+
await jsonfile.writeFile(
48+
pkgFile,
49+
{ ...pkgJson, name, repository, files, version },
50+
{ spaces: 2 }
51+
);
52+
spinner.succeed(`Package.json cooked! ${path.resolve(dest)}`);
53+
} catch (err) {
54+
spinner.fail("Modifying package.json failed.");
55+
throw err;
56+
}
57+
}

packages/cli/src/index.test.js renamed to packages/cli/src/init.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "path";
22

33
import { jsonfile } from "./lib";
4-
import { init } from "./index";
4+
import init from "./init";
55
import download from "./download-npm-package";
66

77
jest.mock("./download-npm-package");

packages/cli/src/lib/git-user.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/cli/src/lib/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as jsonfile from "./jsonfile-then";
2-
import gitUser from "./git-user";
32

4-
export { jsonfile, gitUser };
3+
export { jsonfile };
54
export * from "./utils";

packages/fetch/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"dist"
1616
],
1717
"main": "dist/index.js",
18+
"umd:main": "dist/index.umd.js",
19+
"module": "dist/index.mjs",
1820
"scripts": {
19-
"build": "rm -rf dist && babel ./src -d ./dist --ignore test.js",
21+
"build": "sketch build",
2022
"test": "jest --silent --env=node"
2123
},
2224
"dependencies": {
@@ -25,6 +27,7 @@
2527
"qs": "^6.5.2"
2628
},
2729
"devDependencies": {
30+
"@36node/sketch-cli": "^0.3.3",
2831
"babel-plugin-transform-runtime": "^6.23.0",
2932
"fetch-mock": "^7.0.0-alpha.6",
3033
"jest": "^23.6.0",

0 commit comments

Comments
 (0)