forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
75 lines (69 loc) · 1.92 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as fs from 'fs-extra';
import * as path from 'path';
import { format, resolveConfig } from 'prettier';
export function sortAlphabeticallyFunction(a: string, b: string): number {
const nameA = a.toUpperCase(); // ignore upper and lowercase
const nameB = b.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
}
export async function generateMarkdownFile(
outputDirectory: string,
templateObject: { name: string; template: string }
): Promise<void> {
const filePath = path.join(outputDirectory, `${templateObject.name}.md`);
fs.outputFileSync(
filePath,
await formatWithPrettier(filePath, templateObject.template)
);
}
export async function generateJsonFile(
filePath: string,
json: unknown
): Promise<void> {
fs.outputFileSync(
filePath,
await formatWithPrettier(filePath, JSON.stringify(json))
);
}
export async function formatWithPrettier(filePath: string, content: string) {
let options: any = {
filepath: filePath
};
const resolvedOptions = await resolveConfig(filePath);
if (resolvedOptions) {
options = {
...options,
...resolvedOptions
};
}
return format(content, options);
}
export function getNxPackageDependencies(
packageJsonPath: string
): { name: string; dependencies: string[]; peerDependencies: string[] } {
const packageJson = fs.readJsonSync(packageJsonPath);
if (!packageJson) {
console.log(`No package.json found at: ${packageJsonPath}`);
return null;
}
return {
name: packageJson.name,
dependencies: packageJson.dependencies
? Object.keys(packageJson.dependencies).filter(item =>
item.includes('@nrwl')
)
: [],
peerDependencies: packageJson.peerDependencies
? Object.keys(packageJson.peerDependencies).filter(item =>
item.includes('@nrwl')
)
: []
};
}