Skip to content

Commit

Permalink
🆕chore: refactor the utils (#45)
Browse files Browse the repository at this point in the history
* feat: add preferPackFiles

* fix: fix the preferPackFiles

* feat: add npmPkg meta and add cjsImportEsm

* fix: fix tip and missing export

* chore: delete dead code

* refactor: refactor the code

* fix: fix getSourceFiles

* chore: change npmPkg config field

* chore: change examples

* refactor: refactor the code

* chore: delete the unless code

* chore: recover the .npmrc

* chore: detail adjustment

* chore: detail adjustment
  • Loading branch information
cjhw committed Jul 1, 2023
1 parent 9026e48 commit e03423a
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 98 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ npm run test
```

`examples/diy` 整合了目前已有的绝大部分 rules

```sh
cd examples/diy
npm run test
Expand Down
2 changes: 0 additions & 2 deletions packages/npm-pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
"@umijs/core": "^4.0.71",
"@umijs/utils": "^4.0.71",
"enhanced-resolve": "^5.15.0",
"esbuild": "^0.18.10",
"glob": "^10.3.0",
"lodash": "^4.17.21",
"vm": "^0.1.0"
}
}
15 changes: 11 additions & 4 deletions packages/npm-pkg/src/commands/npm-pkg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { generatePreset } from "@doctors/core";
import type { Nullify } from "@doctors/core";
import { generatePreset, type Nullify } from "@doctors/core";
import { getFilesWithImports } from "@doctors/utils";
import { type IApi, type Meta } from "../type";
import { ConfigSchema } from "../type";
import { PRESET_NAME } from "../constants";
import { getFilesWithImports } from "../utils";

const schema: Nullify<ConfigSchema> = {
npmPkg: {
Expand Down Expand Up @@ -33,7 +32,15 @@ export default async (api: IApi) => {
// meta 元数据 将会作为所有 feature 插件的实参传入 供使用
const meta: Meta = {};

const sourceFiles = await getFilesWithImports(api);
let compileFiles: string[] = [];

if (api.userConfig.npmPkg?.compileFiles) {
compileFiles = Array.isArray(api.userConfig?.npmPkg?.compileFiles)
? api.userConfig?.npmPkg?.compileFiles
: [api.userConfig?.npmPkg?.compileFiles];
}

const sourceFiles = await getFilesWithImports(compileFiles, api.cwd);

meta.sourceFiles = sourceFiles;

Expand Down
7 changes: 0 additions & 7 deletions packages/npm-pkg/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
export const PRESET_NAME = "npm-pkg";
export const DEFAULT_SOURCE_IGNORES = [
"**/.*",
"**/.*/**",
"**/node_modules/**",
"**/dist/**",
"**/build/**",
];
2 changes: 1 addition & 1 deletion packages/npm-pkg/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IApi as DoctorApi, DoctorMeta } from "@doctors/core";
import { DoctorLevel } from "@doctors/core";
import { SourceFile } from "./utils";
import { SourceFile } from "@doctors/utils";

// 元数据
export interface Meta {
Expand Down
84 changes: 0 additions & 84 deletions packages/npm-pkg/src/utils.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"dependencies": {
"@umijs/core": "^4.0.71",
"@umijs/utils": "^4.0.71",
"esbuild": "^0.18.10",
"file-system-cache": "^2.4.1",
"glob": "^10.3.0",
"lodash": "^4.17.21",
"default-browser": "^4.0.0"
}
}
96 changes: 96 additions & 0 deletions packages/utils/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
import { globSync } from "glob";
import path from "path";
import fs from "fs";
import lodash from "lodash";
import sourceParser from "./features/scanningParser";
import { DEFAULT_SOURCE_IGNORES } from "./constants";
import { SourceFile } from "./types";

export function getPkgNameFromPath(p: string) {
return p.match(/^(?:@[a-z\d][\w-.]*\/)?[a-z\d][\w-.]*/i)?.[0];
}

//----------------------------- Source ---------------------------------
export function getSourceDirs(files: string[]) {
const configDirs = lodash.uniq([...files.map((c) => path.dirname(c))]);

return configDirs;
}

export function getSourceFiles(compileFiles: string[], cwd: string) {
let files: string[] = [];

if (compileFiles.length) {
compileFiles.forEach((e) => {
globSync(e, {
cwd,
ignore: DEFAULT_SOURCE_IGNORES,
nodir: true,
}).forEach((file) => {
files.push(file);
});
});
} else {
files = globSync("**/*.{ts,js}", {
cwd,
ignore: DEFAULT_SOURCE_IGNORES,
nodir: true,
});
}

const sourceDirs = getSourceDirs(files);

let allFiles: string[] = sourceDirs.reduce<string[]>(
(ret: string[], dir: string) =>
ret.concat(
globSync(`${dir}/**/*.{ts,js}`, {
cwd,
ignore: DEFAULT_SOURCE_IGNORES,
nodir: true,
})
),
[]
);

allFiles = [...new Set(allFiles)];

return allFiles;
}

export async function getFilesWithImports(
compileFiles: string[],
AbsDir: string
) {
const files: string[] = getSourceFiles(compileFiles, AbsDir);

let sourceFiles: SourceFile[];

sourceFiles = await Promise.all(
files.map(async (file) => {
return {
path: file,
imports: (await sourceParser(path.join(AbsDir, file))).imports,
content: fs.readFileSync(path.join(AbsDir, file), "utf-8"),
};
})
);

return sourceFiles;
}

//----------------------------- MonoRepo ---------------------------------
export function getMonorepoSonPackages(workSpaces: string[], cwd: string) {
const packAgeJsonFiles: string[] = [];
workSpaces.forEach((workSpace) => {
packAgeJsonFiles.push(
...globSync(workSpace + "/**/*.json", {
cwd,
ignore: DEFAULT_SOURCE_IGNORES,
nodir: true,
}).filter((jsonFile) => {
return jsonFile.endsWith("package.json");
})
);
});

const packages = getSourceDirs(packAgeJsonFiles);

return packages;
}
7 changes: 7 additions & 0 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ export const DEFAULT_BUNDLESS_IGNORES = [
"**/*.{test,e2e,spec}.{js,jsx,ts,tsx}",
"**/tsconfig.json",
];
export const DEFAULT_SOURCE_IGNORES = [
"**/.*",
"**/.*/**",
"**/node_modules/**",
"**/dist/**",
"**/build/**",
];
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/utils/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
import { type IDoctorSourceParseResult } from "../features/scanningParser";

export type Awaitable<T> = T | Promise<T>;

export interface SourceFile {
path?: string;
imports?: IDoctorSourceParseResult["imports"];
contents?: string;
}

0 comments on commit e03423a

Please sign in to comment.