Skip to content

Commit

Permalink
feat: design install
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 25, 2020
1 parent 6c9a572 commit df2f71e
Show file tree
Hide file tree
Showing 20 changed files with 1,415 additions and 89 deletions.
4 changes: 3 additions & 1 deletion packages/@textlint/script-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
"@textlint/kernel": "^3.3.6",
"@textlint/runtime-helper": "^0.7.0",
"@textlint/config-partial-parser": "^0.7.0",
"@textlint/script-parser": "^0.7.0",
"@textlint/types": "^1.4.5",
"babel-loader": "^8.1.0",
"babel-plugin-static-fs": "^3.0.0",
"meow": "^7.0.1",
"rimraf": "^3.0.2",
"webpack": "^4.44.0"
"webpack": "^4.44.0",
"read-pkg": "^5.2.0"
},
"devDependencies": {
"@types/mocha": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadConfig, TextlintConfigDescriptor } from "@textlint/config-loader";
import { CodeGeneraterOptions } from "./CodeGeneraterOptions";
import type { TextlintResult, TextlintFixResult } from "@textlint/types";
import { TextlintScriptMetadata } from "@textlint/script-parser";

export type TextlintWorkerCommandLint = {
command: "lint";
Expand All @@ -24,6 +25,7 @@ export type TextlintWorkerCommand =

export type TextlintWorkerCommandResponseInit = {
command: "init";
metadata: TextlintScriptMetadata;
};
export type TextlintWorkerCommandResponseLint = {
command: "lint:result";
Expand Down Expand Up @@ -168,7 +170,8 @@ self.addEventListener('message', (event) => {
});
// ====
self.postMessage({
command: "init"
command: "init",
metadata: process.env.TEXTLINT_SCRIPT_METADATA
});
`;
};
41 changes: 31 additions & 10 deletions packages/@textlint/script-compiler/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import meow from "meow";
import { compile } from "./compiler";
import { compile, validateTextlintScriptMetadata } from "./compiler";
import path from "path";
import readPkg from "read-pkg";

export const cli = meow(
`
Expand All @@ -13,8 +14,16 @@ export const cli = meow(
Default: .textlintrc.{json,yaml,js}
--output-dir [path:String] output file path that is written of reported result.
--mode [String] build mode: "production" or "development"
Metadata Options
Metadata is inferred from package.json by default.
If you want to set metadata by manually, please use theme flags.
--metadataName [String] generated script name
--metadataNamespace [String] generated script namespace
--metadataHomepage [String] generated script homepage url
--metadataVersion [String] generated script version
Examples
$ textlint-script-compiler --output-dir ./dist --metadataName "test" --metadataNamespace "https://example.com"
Expand All @@ -37,12 +46,16 @@ export const cli = meow(
default: "production"
},
metadataName: {
type: "string",
isRequired: true
type: "string"
},
metadataNamespace: {
type: "string",
isRequired: true
type: "string"
},
metadataHomepage: {
type: "string"
},
metadataVersion: {
type: "string"
},
// DEBUG option
cwd: {
Expand All @@ -59,20 +72,28 @@ export const cli = meow(
}
);

export const run = (
export const run = async (
_input = cli.input,
flags = cli.flags
): Promise<{ exitStatus: number; stdout: string | null; stderr: Error | null }> => {
const pkg = await readPkg({
cwd: flags.cwd
});
const metadata = {
name: pkg.name ?? flags["metadataName"],
namespace: pkg.homepage ?? flags["metadataNamespace"],
homepage: pkg.homepage ?? flags["metadataHomepage"],
version: pkg.version ?? flags["metadataVersion"]
};
// assert
validateTextlintScriptMetadata(metadata);
return compile({
configFilePath: flags.textlintrc,
cwd: flags.cwd,
compileTarget: flags.compileTarget as "webworker",
outputDir: path.join(flags.cwd, flags.outputDir),
mode: flags.mode as "production" | "development",
metadata: {
name: flags["metadataName"],
namespace: flags["metadataNamespace"]
}
metadata: metadata
})
.then(() => {
return {
Expand Down
29 changes: 21 additions & 8 deletions packages/@textlint/script-compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ import * as fs from "fs";
import path from "path";
// @ts-ignore
import rimraf from "rimraf";
import type { TextlintScriptMetadata } from "@textlint/script-parser";

export function validateTextlintScriptMetadata(metadata: {}): asserts metadata is Omit<
TextlintScriptMetadata,
"config"
> {
Object.entries(metadata).forEach(([key, value]) => {
if (value === undefined) {
throw new Error(
`metadata.${key} is undefined. please set ${key} metadata in package.json or --metadata${
key[0].toUpperCase() + key.slice(1)
} CLI flag`
);
}
});
}

interface WebpackConfig {
inputFilePath: string;
outputDir: string;
mode: "production" | "development";
metadata: object;
metadata: TextlintScriptMetadata;
}

export const createWebpackConfig = ({
Expand Down Expand Up @@ -58,7 +74,8 @@ export const createWebpackConfig = ({
// https://github.com/azu/kuromojin injection
// 1.x 2.x supports
new webpack.DefinePlugin({
"process.env.KUROMOJIN_DIC_PATH": JSON.stringify("https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict")
"process.env.KUROMOJIN_DIC_PATH": JSON.stringify("https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict"),
"process.env.TEXTLINT_SCRIPT_METADATA": JSON.stringify(metadata)
}),
// kuromoji patch
new webpack.NormalModuleReplacementPlugin(
Expand All @@ -82,10 +99,7 @@ export type compileOptions = {
compileTarget: "webworker";
outputDir: string;
mode: "production" | "development";
metadata: {
name: string;
namespace: string;
};
metadata: Omit<TextlintScriptMetadata, "config">;
} & CodeGeneraterOptions;
export const compile = async (options: compileOptions) => {
const cwd = options.cwd || process.cwd();
Expand Down Expand Up @@ -116,8 +130,7 @@ export const compile = async (options: compileOptions) => {
outputDir: outputFilePath,
mode: options.mode,
metadata: {
name: options.metadata.name,
namespace: options.metadata.namespace,
...options.metadata,
config: configResult.rawConfig
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/script-compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { createWebpackConfig, compile, compileOptions } from "./compiler";
export { createWebpackConfig, compile, compileOptions, validateTextlintScriptMetadata } from "./compiler";
export type {
TextlintWorkerCommand,
TextlintWorkerCommandResponse,
Expand Down
2 changes: 2 additions & 0 deletions packages/@textlint/script-parser/src/script-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type TextlintRcConfig = {
export type TextlintScriptMetadata = {
name: string;
namespace: string;
homepage: string;
version: string;
config: TextlintRcConfig;
};
export type TextlintScriptParseResult = {
Expand Down
38 changes: 28 additions & 10 deletions packages/@textlint/website-generator/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import meow from "meow";
import path from "path";
import { generateWebSite } from "./website-generator";
import readPkg from "read-pkg";
import { validateTextlintScriptMetadata } from "@textlint/script-compiler";

export const cli = meow(
`
Expand All @@ -14,8 +15,16 @@ export const cli = meow(
--cwd current working directory
--textlintrc [path:String] path to .textlintrc file. Default: load .textlintrc.{json,yaml,js}
--output-dir [path:String] output file path that is written of reported result.
--metadataName [String] generated script name
--metadataNamespace [String] generated script namespace
Metadata Options
Metadata is inferred from package.json by default.
If you want to set metadata by manually, please use theme flags.
--metadataName [String] generated script name
--metadataNamespace [String] generated script namespace
--metadataHomepage [String] generated script homepage url
--metadataVersion [String] generated script version
Examples
$ textlint-website-generator --output-dir ./dist --metadataName "script name" --metadataNamespace "https://example.com"
Expand All @@ -41,12 +50,16 @@ export const cli = meow(
default: "production"
},
metadataName: {
type: "string",
isRequired: true
type: "string"
},
metadataNamespace: {
type: "string",
isRequired: true
type: "string"
},
metadataHomepage: {
type: "string"
},
metadataVersion: {
type: "string"
},
// DEBUG option
cwd: {
Expand All @@ -70,6 +83,14 @@ export const run = async (
const pkg = await readPkg({
cwd: flags.cwd
});
const metadata = {
name: pkg.name ?? flags["metadataName"],
namespace: pkg.homepage ?? flags["metadataNamespace"],
homepage: pkg.homepage ?? flags["metadataHomepage"],
version: pkg.version ?? flags["metadataVersion"]
};
// assert
validateTextlintScriptMetadata(metadata);
return generateWebSite({
title: flags.title ?? (pkg.name as string),
placeholder: flags.placeholder ?? "",
Expand All @@ -78,10 +99,7 @@ export const run = async (
compileTarget: "webworker",
outputDir: path.join(flags.cwd, flags.outputDir),
mode: (flags.mode as "production" | "development") ?? "production",
metadata: {
name: flags["metadataName"],
namespace: flags["metadataNamespace"]
}
metadata
})
.then(() => {
return {
Expand Down
5 changes: 5 additions & 0 deletions packages/textchecker-element/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/env"
]
}
4 changes: 3 additions & 1 deletion packages/textchecker-element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
],
"scripts": {
"compile:textlint": "textlint-script-compiler --outputDir public --metadataName \"example\" --metadataNamespace \"https://github.com/textlint/editor\"",
"predev": "npm run compile:textlint",
"cp-textlintscript": "cp public/textlint.js dist/",
"predev": "npm run compile:textlint && npm run cp-textlintscript",
"dev": "parcel serve public/index.html",
"preweb": "npm run compile:textlint",
"web": "parcel build public/index.html",
Expand All @@ -53,6 +54,7 @@
"trailingComma": "none"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@textlint/script-compiler": "^0.7.0",
"@types/mocha": "^8.0.0",
"@types/node": "^14.0.26",
Expand Down
Loading

0 comments on commit df2f71e

Please sign in to comment.