MCPcopy Create free account
hub / github.com/Snapchat/Valdi / createProject

Function createProject

npm_modules/cli/test/helpers/ProjectHelpers.ts:27–81  ·  view source on GitHub ↗
(name: string, rootPath: string)

Source from the content-addressed store, hash-verified

25}
26
27export function createProject(name: string, rootPath: string): ProjectConfig {
28 const findFile: ProjectConfig['findFile'] = (fileName, withinModule) => {
29 return new Promise((resolve, reject) => {
30 const buffers: Buffer[] = [];
31
32 const errorBuffers: Buffer[] = [];
33 const directory = path.join(...['.', 'modules'].concat(withinModule ?? []));
34
35 const child = spawn('find', [directory]);
36 child.stdout.on('data', (chunk: Buffer) => {
37 buffers.push(chunk);
38 });
39
40 child.stderr.on('data', (chunk: Buffer) => {
41 errorBuffers.push(chunk);
42 });
43
44 child.on('close', code => {
45 if (!!code && code > 0) {
46 reject(new Error('exited with error code: ' + code.toString()));
47 }
48
49 const output = Buffer.concat(buffers).toString('utf8');
50
51 const files = output.trim().split('\n');
52
53 const found = files.find(file => file.endsWith(`/${fileName}`));
54
55 if (!found) {
56 reject(new Error('File not found: ' + fileName));
57 return;
58 }
59
60 resolve(path.join(rootPath, found));
61 });
62 });
63 };
64 return {
65 name,
66 rootPath,
67 findFile,
68 updateFile: async (file, withinModule, updater) => {
69 const path = await findFile(file, withinModule);
70 const contents = await readFile(path);
71 const updated = await updater(contents);
72 await writeFile(path, updated);
73 },
74 createFile: async (filePath, withinModule, source) => {
75 const fullPath = path.resolve(path.join(rootPath, withinModule ? `modules/${withinModule}` : '.', filePath));
76 await mkdir(path.dirname(fullPath), { recursive: true });
77 await writeFile(fullPath, source);
78 return fullPath;
79 },
80 };
81}
82
83type LineMatcher = (line: string) => boolean;
84

Callers 1

ValdiSmokeTest.tsFile · 0.90

Calls 3

findFileFunction · 0.85
resolveMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected