Skip to content

Commit

Permalink
feat: Improvement api
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Mar 2, 2023
1 parent 7a10cb2 commit c20d095
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 168 deletions.
79 changes: 39 additions & 40 deletions pnpm-lock.yaml

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

22 changes: 18 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ export interface DefaultOption {
};
}

export type CommandsOption = Omit<DefaultOption, "path"> & { path: string };
export interface Config {
export type CommandsOption = Omit<DefaultOption, "path">;

export interface Output {
warn(message: string): void;
error(message: string): void;
log(message: string): void;
success(message: string): void;
}

export interface Handlers {
parseSync: ParseSyncType;
output: Output;
}

export type SfcOptions = CommandsOption & Handlers;

export type ScriptOptions = {
fileType: FileType;
script: string;
offset: number;
fileAbsolutePath: string;
propsNotOnlyTs?: boolean;
}
} & Handlers;

export const parseOption = {
target: "es2022",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { DefaultOption } from "./constants";

export { transformSfc, transformScript } from "./transform";

export { Output, SfcOptions, ScriptOptions } from "./constants";

export function defineConfig(option: DefaultOption) {
return option;
}
20 changes: 15 additions & 5 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { findUpSync } from "find-up";
import { getTheFileAbsolutePath, output, useConfigPath } from "./utils";
import { getTheFileAbsolutePath, useConfigPath } from "./utils";
import { transformSfc } from "./transform";
import { CommandsOption } from "./constants";
import writeFile from "./writeFile";
import { readFileSync } from "fs";
import { parseSync } from "@swc/core";
import { blue, green, red, yellow } from "colorette";

const CONFIG_FILE_NAME = "tosetup.config" as const;

Expand Down Expand Up @@ -47,8 +49,8 @@ async function setup() {
`${CONFIG_FILE_NAME}.ts`,
]);
if (!configPath) {
output.error(
`Please enter a file path or use a ${CONFIG_FILE_NAME} file.`,
console.error(
red(`Please enter a file path or use a ${CONFIG_FILE_NAME} file.`),
);
process.exit(1);
}
Expand All @@ -61,9 +63,17 @@ async function setup() {
}

for (const path of pathNames) {
const output = {
warn: (message: string) =>
console.log(`${yellow(message)} in the ${path}`),
error: (message: string) => console.log(`${red(message)} in the ${path}`),
log: (message: string) => console.log(`${blue(message)} in the ${path}`),
success: (message: string) =>
console.log(`${green(message)} in the ${path}`),
};
output.log(`File ${path} start of transform...`);
const sfc = readFileSync(path).toString();
const code = transformSfc(sfc, { ...commands, path });
const code = transformSfc(sfc, { ...commands, parseSync, output });
if (code) {
try {
const file = writeFile(code, path, commands);
Expand All @@ -75,7 +85,7 @@ async function setup() {
}
}

output.log(`Done in ${Math.floor(Date.now() - start)}ms.`);
console.log(`Done in ${Math.floor(Date.now() - start)}ms.`);
}

setup();
8 changes: 4 additions & 4 deletions src/transform/attrsAndSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import type {
ImportDeclaration,
ImportSpecifier,
} from "@swc/core";
import { Config, SetupAst } from "../constants";
import { ScriptOptions, SetupAst } from "../constants";
import { getRealSpan, getSetupSecondParams } from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

function transformAttrsAndSlots(
setupAst: SetupAst,
{ offset, fileAbsolutePath }: Config,
{ offset, output }: ScriptOptions,
) {
const attrsName = getSetupSecondParams("attrs", setupAst, fileAbsolutePath);
const slotsName = getSetupSecondParams("slots", setupAst, fileAbsolutePath);
const attrsName = getSetupSecondParams("attrs", setupAst, output);
const slotsName = getSetupSecondParams("slots", setupAst, output);
if (!(attrsName || slotsName)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/transform/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type {
Identifier,
ObjectExpression,
} from "@swc/core";
import { Config, SetupAst } from "../constants";
import { ScriptOptions, SetupAst } from "../constants";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";
import { getRealSpan } from "../utils";

function transformComponents(
componentsAst: ArrayExpression | Identifier | ObjectExpression,
_setupAst: SetupAst,
config: Config,
config: ScriptOptions,
) {
const { script, offset } = config;
if (
Expand Down
12 changes: 5 additions & 7 deletions src/transform/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
NamedImportSpecifier,
ObjectExpression,
} from "@swc/core";
import { Config, SetupAst } from "../constants";
import { getRealSpan, output } from "../utils";
import { ScriptOptions, SetupAst } from "../constants";
import { getRealSpan } from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

Expand All @@ -17,16 +17,14 @@ function transformDirectiveName(name: string) {
function transformDirectives(
directivesAst: Identifier | ObjectExpression,
_setupAst: SetupAst,
config: Config,
config: ScriptOptions,
) {
const { script, offset, fileAbsolutePath } = config;
const { script, offset, output } = config;
if (
directivesAst.type === "Identifier" ||
directivesAst.properties.some((ast) => ast.type === "SpreadElement")
) {
output.warn(
`Please manually modify the custom directives in ${fileAbsolutePath}.`,
);
output.warn("Please manually modify the custom directives");
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/transform/emits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
BlockStatement,
} from "@swc/core";

import { Config, SetupAst } from "../constants";
import { ScriptOptions, SetupAst } from "../constants";
import {
GetCallExpressionFirstArg,
getRealSpan,
Expand All @@ -21,10 +21,10 @@ import type MagicString from "magic-string";
function transformEmits(
emitsAst: ArrayExpression | ObjectExpression | Identifier | null,
setupAst: SetupAst,
config: Config,
config: ScriptOptions,
) {
const { script, offset, fileAbsolutePath } = config;
const name = getSetupSecondParams("emit", setupAst, fileAbsolutePath);
const { script, offset, output } = config;
const name = getSetupSecondParams("emit", setupAst, output);
if (!name) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/transform/expose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, SetupAst } from "../constants";
import { ScriptOptions, SetupAst } from "../constants";
import {
GetCallExpressionFirstArg,
getRealSpan,
Expand All @@ -14,9 +14,9 @@ import type {
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

function transformExpose(setupAst: SetupAst, config: Config) {
const { script, offset, fileAbsolutePath } = config;
const name = getSetupSecondParams("expose", setupAst, fileAbsolutePath);
function transformExpose(setupAst: SetupAst, config: ScriptOptions) {
const { script, offset, output } = config;
const name = getSetupSecondParams("expose", setupAst, output);
if (!name) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/transform/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import type {
NamedImportSpecifier,
ObjectExpression,
} from "@swc/core";
import { Config, FileType, SetupAst } from "../constants";
import { ScriptOptions, FileType, SetupAst } from "../constants";
import { getPropsValue, getRealSpan, getSpecifierOffset } from "../utils";
import { Visitor } from "@swc/core/Visitor.js";
import type MagicString from "magic-string";

function transformProps(
propsAst: ArrayExpression | Identifier | ObjectExpression,
setupAst: SetupAst,
config: Config,
config: ScriptOptions,
) {
const { script, offset, fileType, propsNotOnlyTs } = config;

Expand Down
Loading

0 comments on commit c20d095

Please sign in to comment.