@10stars/config 11.0.2 → 11.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +4 -9
- package/src/link-packages.ts +46 -47
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { execSync } from "node:child_process"
|
|
2
2
|
import { join } from "node:path"
|
|
3
3
|
|
|
4
|
-
import { linkPackages } from "./link-packages"
|
|
4
|
+
import { linkPackages, checkIsMonorepo } from "./link-packages"
|
|
5
5
|
|
|
6
6
|
const cwd = process.env.PWD!
|
|
7
7
|
|
|
8
8
|
interface Config {
|
|
9
|
-
linkPackages:
|
|
9
|
+
linkPackages: string[]
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const exec = (cmd: string) => {
|
|
@@ -21,13 +21,8 @@ const getConfig = async (): Promise<Config> =>
|
|
|
21
21
|
)
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
-
const checkIsMonorepo = async () => {
|
|
25
|
-
const pkgJson = await import(join(cwd, "package.json"))
|
|
26
|
-
return Boolean(pkgJson.workspaces)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
24
|
const getESLintPaths = async () => {
|
|
30
|
-
const isMonorepo = await checkIsMonorepo()
|
|
25
|
+
const isMonorepo = await checkIsMonorepo(cwd)
|
|
31
26
|
return isMonorepo ? `*/src */*.ts` : `src *.ts`
|
|
32
27
|
}
|
|
33
28
|
|
|
@@ -47,7 +42,7 @@ const actions = {
|
|
|
47
42
|
format: {
|
|
48
43
|
info: "Format code",
|
|
49
44
|
action: async () => {
|
|
50
|
-
const isMonorepo = await checkIsMonorepo()
|
|
45
|
+
const isMonorepo = await checkIsMonorepo(cwd)
|
|
51
46
|
exec(
|
|
52
47
|
`prettier -l --write './${isMonorepo ? `*/` : ``}src/**/*.{t,j}s{x,}'`,
|
|
53
48
|
)
|
package/src/link-packages.ts
CHANGED
|
@@ -1,76 +1,75 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
realpath,
|
|
4
|
-
rm,
|
|
5
|
-
unlink,
|
|
6
|
-
symlink as symlinkRaw,
|
|
7
|
-
readdir,
|
|
8
|
-
} from "node:fs/promises"
|
|
9
|
-
import { join } from "node:path"
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import path from "node:path"
|
|
10
3
|
|
|
11
4
|
import type * as TF from "type-fest"
|
|
12
5
|
|
|
13
6
|
const symlink = async (pathTarget: string, pathSymlink: string) => {
|
|
14
|
-
const stats = await lstat(pathSymlink).catch(() => null)
|
|
7
|
+
const stats = await fs.lstat(pathSymlink).catch(() => null)
|
|
15
8
|
if (!stats) return
|
|
16
9
|
|
|
17
10
|
if (stats.isSymbolicLink()) {
|
|
18
|
-
const symlinkRealpath = await realpath(pathSymlink).catch(() => null)
|
|
11
|
+
const symlinkRealpath = await fs.realpath(pathSymlink).catch(() => null)
|
|
19
12
|
if (pathTarget === symlinkRealpath) return
|
|
20
13
|
}
|
|
21
14
|
|
|
22
|
-
if (stats.isDirectory()) await rm(pathSymlink, { recursive: true })
|
|
23
|
-
else await unlink(pathSymlink)
|
|
15
|
+
if (stats.isDirectory()) await fs.rm(pathSymlink, { recursive: true })
|
|
16
|
+
else await fs.unlink(pathSymlink)
|
|
24
17
|
|
|
25
|
-
await
|
|
18
|
+
await fs.symlink(pathTarget, pathSymlink)
|
|
26
19
|
}
|
|
27
20
|
|
|
28
|
-
export const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
export const checkIsMonorepo = async (pathPkg: string) => {
|
|
22
|
+
const pkgJson = await import(path.join(pathPkg, "package.json"))
|
|
23
|
+
return Boolean(pkgJson.workspaces)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const linkPackages = async (cwd: string, packagesToLink: string[]) => {
|
|
32
27
|
console.info(`Linking local packages`)
|
|
33
28
|
|
|
34
29
|
const linkPackage = async (pathPkg: string) => {
|
|
35
30
|
const pkgJson: TF.PackageJson = await import(
|
|
36
|
-
join(pathPkg, `package.json`)
|
|
31
|
+
path.join(pathPkg, `package.json`)
|
|
37
32
|
).catch(() => null)
|
|
38
33
|
if (!pkgJson) return
|
|
39
34
|
|
|
40
35
|
const [orgName, pkgName] = pkgJson.name!.split(`/`)
|
|
41
36
|
|
|
42
|
-
const pathSymlink = join(cwd, `node_modules`, orgName, pkgName)
|
|
37
|
+
const pathSymlink = path.join(cwd, `node_modules`, orgName, pkgName)
|
|
43
38
|
return symlink(pathPkg, pathSymlink)
|
|
44
39
|
}
|
|
45
40
|
|
|
46
41
|
const promises: Promise<unknown>[] = []
|
|
47
|
-
for (const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
42
|
+
for (const relativePath of packagesToLink) {
|
|
43
|
+
const pathPkg = path.join(cwd, relativePath)
|
|
44
|
+
const promise = checkIsMonorepo(pathPkg).then(async (isMonorepo) => {
|
|
45
|
+
if (!isMonorepo) return linkPackage(pathPkg)
|
|
46
|
+
|
|
47
|
+
const files = await fs
|
|
48
|
+
.readdir(relativePath, { withFileTypes: true })
|
|
49
|
+
.catch(() => null)
|
|
50
|
+
if (!files) return
|
|
51
|
+
|
|
52
|
+
const directories = files
|
|
53
|
+
.filter((v) => v.isDirectory())
|
|
54
|
+
.map((v) => v.name)
|
|
55
|
+
const packages = (
|
|
56
|
+
await Promise.all(
|
|
57
|
+
directories.map(async (v) => {
|
|
58
|
+
const pathPkg = path.join(cwd, relativePath, v)
|
|
59
|
+
const pkgJson: TF.PackageJson = await import(
|
|
60
|
+
path.join(pathPkg, `package.json`)
|
|
61
|
+
).catch(() => null)
|
|
62
|
+
if (pkgJson) return v
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
).filter(Boolean)
|
|
66
|
+
for (const pkg of packages) {
|
|
67
|
+
const relativePathToPkg = path.join(cwd, relativePath, pkg!)
|
|
68
|
+
promises.push(linkPackage(relativePathToPkg))
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
promises.push(promise)
|
|
74
73
|
}
|
|
75
74
|
|
|
76
75
|
await Promise.all(promises)
|