Skip to content

Commit

Permalink
feat(shared): add isSubPackageMode
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Aug 29, 2023
1 parent 01e4e37 commit 7b38608
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/_/monorepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"cli": "../../../node_modules/.bin/tsx ../../cli/src/index.ts",
"dev": "npm run cli -- dev",
"dev": "npm run cli -- dev --dry-run",
"build": "npm run cli -- build",
"test": "npm run cli -- test --package-name '*'",
"release": "npm run cli -- release",
Expand Down
1 change: 1 addition & 0 deletions packages/_/monorepo/packages/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TEST } from '../index';

describe('index.ts', () => {
it('any', () => {
expect(TEST).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TEST } from '../index';

describe('index.ts', () => {
it('any', () => {
expect(TEST).toBe(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(2);
1 change: 1 addition & 0 deletions packages/_/monorepo/packages/components/button/index.m.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TEST = 1;
1 change: 1 addition & 0 deletions packages/_/monorepo/packages/components/button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TEST = 1;
1 change: 1 addition & 0 deletions packages/_/monorepo/packages/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TEST = 2;
18 changes: 18 additions & 0 deletions packages/_/monorepo/packages/components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@demo/helper-components",
"version": "0.0.1",
"type": "module",
"main": "dist/index.es.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"files": [
"dist"
],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {}
}
1 change: 1 addition & 0 deletions packages/_/monorepo/packages/index/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@demo/helper",
"version": "1.0.0",
"type": "module",
"main": "dist/index.es.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
2 changes: 2 additions & 0 deletions packages/_/monorepo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"@deot/dev": ["../../../packages/index/src"],
"@deot/dev-*": ["../../../packages/*/src"],
"@demo/helper": ["packages/index/src"],
"@demo/helper-components": ["packages/components"],
"@demo/helper-*": ["packages/*/src"]
}
},
"include": [
"packages/shims.d.ts",
"packages/components/*/__tests__",
"packages/*/__tests__",
"packages/*/src"
],
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/__tests__/locals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ describe('shared.ts', () => {
expect(it.packageDir).toMatch(`/shared`);
expect(it.packageFolderNames.length).toBe(0);
});

it('subpackage', () => {
expect(Locals.isSubPackageMode('index')).toBe(false);
expect(Locals.isSubPackageMode('_/monorepo/packages/components')).toBe(true);
});
});
20 changes: 20 additions & 0 deletions packages/shared/src/locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ export const getNormalizePackage = (dataMap: any) => {
return result.reverse();
};

export const isSubPackageMode = (subdir: string) => {
const { workspace, packageDir } = impl();
/* istanbul ignore next -- @preserve */
if (!workspace) return false;

let dir = path.resolve(packageDir, subdir);

// 根目录含index.ts, 不含src, 有文件夹且含__tests__,即认为当前为子包
return fs.existsSync(`${dir}/index.ts`)
&& !fs.existsSync(`${dir}/src`)
&& fs.readdirSync(dir).some((file: string) => {
const fullpath = path.join(dir, file);
const stat = fs.statSync(fullpath);
if (stat.isDirectory()) {
return fs.existsSync(`${fullpath}/__tests__`);
}
return false;
});
};

export const getPackageName = (packageFolderName$: string) => {
const { workspace, packageFolderName, packageName } = impl();
if (
Expand Down

0 comments on commit 7b38608

Please sign in to comment.