@procamp/cli 0.0.4 → 0.0.6
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/dist/bin/index.js +13 -1
- package/dist/bin/index.js.map +1 -1
- package/dist/command/publish.d.ts +1 -1
- package/dist/command/publish.js +19 -9
- package/dist/command/publish.js.map +1 -1
- package/dist/interfaces/course.d.ts +1 -0
- package/dist/interfaces/course.js.map +1 -1
- package/dist/utils/writeJson.d.ts +7 -0
- package/dist/utils/writeJson.js +53 -0
- package/dist/utils/writeJson.js.map +1 -0
- package/package.json +1 -1
package/dist/bin/index.js
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
#! /usr/bin/env node
|
2
2
|
"use strict";
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
10
|
+
});
|
11
|
+
};
|
3
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
4
13
|
exports.program = void 0;
|
5
14
|
const commander_1 = require("commander");
|
@@ -32,7 +41,10 @@ exports.program
|
|
32
41
|
exports.program
|
33
42
|
.command('publish')
|
34
43
|
.description('Publish file to service')
|
35
|
-
.
|
44
|
+
.option('-a, --access <type>', 'Set access type (public or private)')
|
45
|
+
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
|
46
|
+
yield (0, publish_1.publishCourse)(options.access);
|
47
|
+
}));
|
36
48
|
exports.program
|
37
49
|
.command('validate')
|
38
50
|
.description('Validate course folder structure')
|
package/dist/bin/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bin/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bin/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAEA,yCAAoC;AACpC,kDAAqD;AACrD,0CAA6C;AAC7C,4CAAyC;AACzC,wCAA8C;AAC9C,0CAA4C;AAC5C,gDAAmD;AACnD,kDAA+C;AAC/C,4CAAwC;AACxC,wCAA2E;AAE3E,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAExD,QAAA,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;AACrC,eAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;AAE7D,eAAO;KACJ,OAAO,CAAC,MAAM,CAAC;KACf,WAAW,CAAC,mCAAmC,CAAC;KAChD,MAAM,CACL,6BAA6B,EAC7B,iCAAiC,EACjC,OAAO,CAAC,GAAG,EAAE,CACd;KACA,MAAM,CAAC,iBAAU,CAAC,CAAC;AAEtB,eAAO;KACJ,OAAO,CAAC,mBAAmB,CAAC;KAC5B,WAAW,CAAC,yBAAyB,CAAC;KACtC,MAAM,CAAC,kBAAY,CAAC,CAAC;AAExB,eAAO;KACJ,OAAO,CAAC,MAAM,CAAC;KACf,WAAW,CAAC,gBAAgB,CAAC;KAC7B,MAAM,CAAC,6BAA6B,EAAE,uBAAuB,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC;KAC7E,MAAM,CAAC,gBAAS,CAAC,CAAC;AAErB,eAAO;KACJ,OAAO,CAAC,SAAS,CAAC;KAClB,WAAW,CAAC,yBAAyB,CAAC;KACtC,MAAM,CAAC,qBAAqB,EAAE,qCAAqC,CAAC;KACpE,MAAM,CAAC,CAAO,O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C/C,eAAO,CAAC,IAAI,EAAE,CAAC;IACf,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC;AACH,eAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC","sourcesContent":["#! /usr/bin/env node\n\nimport { Command } from 'commander';\nimport { generateCourse } from '../command/generate';\nimport { initCourse } from '../command/init';\nimport { login } from '../command/login';\nimport { createCourse } from '../command/new';\nimport { zipCourse } from '../command/pack';\nimport { publishCourse } from '../command/publish';\nimport { validate } from '../command/validate';\nimport { error } from '../utils/output';\nimport { setClientId, setClientSecret, setCourseId } from '../command/set';\n\nconst { version, name, description } = require('../../package.json');\n\nexport const program = new Command();\nprogram.name(name).version(version).description(description);\n\nprogram\n .command('init')\n .description('Initialise project configuration ')\n .option(\n '-d, --directory <directory>',\n 'Directory to initialise project',\n process.cwd()\n )\n .action(initCourse);\n\nprogram\n .command('new <projectName>')\n .description('Generate course project')\n .action(createCourse);\n\nprogram\n .command('pack')\n .description('Compress files')\n .option('-d, --directory <directory>', 'Directory to compress', process.cwd())\n .action(zipCourse);\n\nprogram\n .command('publish')\n .description('Publish file to service')\n .option('-a, --access <type>', 'Set access type (public or private)')\n .action(async (options) => {\n await publishCourse(options.access);\n });\nprogram\n .command('validate')\n .description('Validate course folder structure')\n .action(validate);\n\n// Commande pour générer un chapitre\nprogram\n .command('generate <key> <value>')\n .alias('g')\n .description('Generating Course with blueprint.yml')\n .action((key: string, value: string) => {\n if (key === 'course') return generateCourse(value);\n\n return error('unknown command ');\n });\n\nprogram\n .command('set <key> <value>')\n .description('set value')\n .action((key: string, value: string) => {\n if (key === 'courseId') return setCourseId(value);\n else if (key === 'clientId') return setClientId(value);\n else if (key === 'clientSecret') return setClientSecret(value);\n return error('unknown command ');\n });\n\nprogram.command('login').description('Login to procamp').action(login);\nprogram.on('command:*', ([command]) => {\n error(`Erreur : unknown \"${command}\" command`);\n program.help();\n process.exit(1);\n});\nprogram.parse(process.argv);\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
export declare function publishCourse(): Promise<void>;
|
1
|
+
export declare function publishCourse(access: string): Promise<void>;
|
package/dist/command/publish.js
CHANGED
@@ -43,14 +43,30 @@ const fs = __importStar(require("fs-extra"));
|
|
43
43
|
const os = __importStar(require("os"));
|
44
44
|
const path = __importStar(require("path"));
|
45
45
|
const output_1 = require("../utils/output");
|
46
|
+
const writeJson_1 = require("../utils/writeJson");
|
46
47
|
const pack_1 = require("./pack");
|
47
|
-
function publishCourse() {
|
48
|
+
function publishCourse(access) {
|
48
49
|
return __awaiter(this, void 0, void 0, function* () {
|
49
50
|
const directory = process.cwd();
|
50
51
|
const filenameConf = path.join(directory, 'meta.json');
|
51
52
|
if (yield fs.exists(filenameConf)) {
|
52
53
|
const data = yield fs.readFile(filenameConf, { encoding: 'utf-8' });
|
53
|
-
|
54
|
+
let meta = JSON.parse(data);
|
55
|
+
meta.public = !!meta.public;
|
56
|
+
if (access) {
|
57
|
+
switch (access.toLowerCase()) {
|
58
|
+
case 'public':
|
59
|
+
meta.public = true;
|
60
|
+
break;
|
61
|
+
case 'private':
|
62
|
+
meta.public = false;
|
63
|
+
break;
|
64
|
+
}
|
65
|
+
}
|
66
|
+
yield (0, writeJson_1.writeJson)({
|
67
|
+
directory,
|
68
|
+
data: meta
|
69
|
+
});
|
54
70
|
if (meta.courseId && meta.courseId.length !== 0) {
|
55
71
|
const courseId = meta.courseId;
|
56
72
|
const filePathZip = path.join(directory, '.output.zip');
|
@@ -98,13 +114,7 @@ function publishCourse() {
|
|
98
114
|
});
|
99
115
|
}
|
100
116
|
});
|
101
|
-
|
102
|
-
yield (0, pack_1.zipCourse)({
|
103
|
-
directory: directory
|
104
|
-
});
|
105
|
-
yield postZipfile();
|
106
|
-
return;
|
107
|
-
}
|
117
|
+
yield (0, pack_1.zipCourse)({ directory: directory });
|
108
118
|
return yield postZipfile();
|
109
119
|
}
|
110
120
|
else {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"publish.js","sourceRoot":"","sources":["../../src/command/publish.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAA2D;AAC3D,kDAA0B;AAC1B,0DAAiC;AACjC,6CAA+B;AAC/B,uCAAyB;AACzB,2CAA6B;
|
1
|
+
{"version":3,"file":"publish.js","sourceRoot":"","sources":["../../src/command/publish.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gEAA2D;AAC3D,kDAA0B;AAC1B,0DAAiC;AACjC,6CAA+B;AAC/B,uCAAyB;AACzB,2CAA6B;AAG7B,4CAAwC;AACxC,kDAA+C;AAC/C,iCAAmC;AAEnC,SAAsB,aAAa,CAAC,MAAc;;QAChD,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAChC,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QAEvD,IAAI,MAAM,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE;YACjC,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;YACpE,IAAI,IAAI,GAAe,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACxC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC;YAE5B,IAAI,MAAM,EAAE;gBACV,QAAQ,MAAM,CAAC,WAAW,EAAE,EAAE;oBAC5B,KAAK,QAAQ;wBACX,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;wBACnB,MAAM;oBACR,KAAK,SAAS;wBACZ,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;wBACpB,MAAM;iBACT;aACF;YAED,MAAM,IAAA,qBAAS,EAAC;gBACd,SAAS;gBACT,IAAI,EAAE,IAAI;aACX,CA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CAAC,CAAC;wBAChC,eAAK;6BACF,IAAI,CACH,kDAAkD,QAAQ,EAAE,EAC5D,IAAI,EACJ;4BACE,OAAO,EAAE;gCACP,WAAW,EAAE,aAAa,CAAC,SAAS,CAAC;6BACtC;yBACF,CACF;6BACA,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;4BACjB,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;wBAC7B,CAAC,CAAC;6BACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;4BACb,IAAI,UAAU,IAAI,GAAG;gCACnB,IAAI,MAAM,IAAI,GAAG,CAAC,QAAQ,EAAE;oCAC1B,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC;oCAC/B,IAAI,YAAY,IAAI,IAAI,IAAI,SAAS,IAAI,IAAI;wCAC3C,IAAA,cAAK,EACH,2BAA2B;4CACzB,IAAI,CAAC,OAAO;4CACZ,UAAU;4CACV,IAAI,CAAC,UAAU,CAClB,CAAC;iCACL;wBACL,CAAC,CAAC,CAAC;qBACN;gBACH,CAAC,CAAA,CAAC;gBAEF,MAAM,IAAA,gBAAS,EAAC,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;gBAE1C,OAAO,MAAM,WAAW,EAAE,CAAC;aAC5B;iBAAM;gBACL,OAAO,IAAA,cAAK,EACV,mDAAmD;oBACjD,2BAA2B,CAC9B,CAAC;aACH;SACF;aAAM;YACL,OAAO,IAAA,cAAK,EAAC,+BAA+B,CAAC,CAAC;SAC/C;IACH,CAAC;CAAA;AA7FD,sCA6FC","sourcesContent":["import { validateCourse } from '@procamp/course-validator';\nimport axios from 'axios';\nimport FormData from 'form-data';\nimport * as fs from 'fs-extra';\nimport * as os from 'os';\nimport * as path from 'path';\nimport { courseInfo } from '../interfaces/course';\nimport { procampConfig } from '../interfaces/procamp_config';\nimport { error } from '../utils/output';\nimport { writeJson } from '../utils/writeJson';\nimport { zipCourse } from './pack';\n\nexport async function publishCourse(access: string) {\n const directory = process.cwd();\n const filenameConf = path.join(directory, 'meta.json');\n\n if (await fs.exists(filenameConf)) {\n const data = await fs.readFile(filenameConf, { encoding: 'utf-8' });\n let meta: courseInfo = JSON.parse(data);\n meta.public = !!meta.public;\n\n if (access) {\n switch (access.toLowerCase()) {\n case 'public':\n meta.public = true;\n break;\n case 'private':\n meta.public = false;\n break;\n }\n }\n\n await writeJson({\n directory,\n data: meta\n });\n\n if (meta.courseId && meta.courseId.length !== 0) {\n const courseId: string = meta.courseId;\n const filePathZip = path.join(directory, '.output.zip');\n const configPath = path.join(os.homedir(), '.procamp-config.json');\n let configContent: procampConfig;\n if (await fs.exists(configPath)) {\n try {\n configContent = await fs.readJSON(configPath);\n } catch (e) {\n throw e;\n }\n if (!('userId' in meta) || meta.userId.length !== 0) {\n if (configContent.userId)\n await fs.writeJson(\n filenameConf,\n { ...meta, userId: configContent['userId'] },\n { spaces: 2 }\n );\n }\n } else return error(\"I don't find config file.Please log in \");\n\n await validateCourse(directory);\n const postZipfile = async () => {\n if (await fs.exists(filePathZip)) {\n const fileStream = fs.createReadStream(filePathZip);\n const form = new FormData();\n form.append('file', fileStream);\n axios\n .post(\n `https://course-manager.procamp.dev/api/courses/${courseId}`,\n form,\n {\n headers: {\n 'x-api-key': configContent['api_key']\n }\n }\n )\n .then((response) => {\n console.log(response.data);\n })\n .catch((err) => {\n if ('response' in err)\n if ('data' in err.response) {\n const data = err.response.data;\n if ('statusCode' in data && 'message' in data)\n error(\n 'Error publishing folder: ' +\n data.message +\n '. Error ' +\n data.statusCode\n );\n }\n });\n }\n };\n\n await zipCourse({ directory: directory });\n\n return await postZipfile();\n } else {\n return error(\n \"Course Id key don't exist or null in meta.json.\\n\" +\n 'Please complete course id'\n );\n }\n } else {\n return error('meta.json file does not exist');\n }\n}\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"course.js","sourceRoot":"","sources":["../../src/interfaces/course.ts"],"names":[],"mappings":"","sourcesContent":["export interface assessments {\n title: string;\n description: string;\n language: string;\n solution: boolean;\n}\nexport interface lessons {\n title: string;\n objectives: string[];\n assessments?: assessments[];\n}\nexport interface chapters {\n title: string;\n lessons: lessons[];\n}\n\nexport interface courseInfo {\n title: string;\n author: string;\n description: string;\n version: string;\n courseId: string;\n userId?: string;\n}\nexport interface course {\n course: courseInfo;\n chapters: chapters[];\n}\n"]}
|
1
|
+
{"version":3,"file":"course.js","sourceRoot":"","sources":["../../src/interfaces/course.ts"],"names":[],"mappings":"","sourcesContent":["export interface assessments {\n title: string;\n description: string;\n language: string;\n solution: boolean;\n}\nexport interface lessons {\n title: string;\n objectives: string[];\n assessments?: assessments[];\n}\nexport interface chapters {\n title: string;\n lessons: lessons[];\n}\n\nexport interface courseInfo {\n title: string;\n author: string;\n description: string;\n version: string;\n courseId: string;\n userId?: string;\n public?: boolean;\n}\nexport interface course {\n course: courseInfo;\n chapters: chapters[];\n}\n"]}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
+
if (mod && mod.__esModule) return mod;
|
20
|
+
var result = {};
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
+
__setModuleDefault(result, mod);
|
23
|
+
return result;
|
24
|
+
};
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
|
+
});
|
33
|
+
};
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
35
|
+
exports.writeJson = void 0;
|
36
|
+
const fs = __importStar(require("fs-extra"));
|
37
|
+
const path = __importStar(require("path"));
|
38
|
+
function writeJson(args) {
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
40
|
+
const { directory, data, fileName = 'meta.json' } = args;
|
41
|
+
const filePath = path.join(directory, fileName);
|
42
|
+
const fileExists = yield fs.pathExists(filePath);
|
43
|
+
let currentData = {};
|
44
|
+
if (fileExists) {
|
45
|
+
const rawData = yield fs.readFile(filePath, { encoding: 'utf-8' });
|
46
|
+
currentData = JSON.parse(rawData);
|
47
|
+
}
|
48
|
+
const newData = Object.assign(Object.assign({}, currentData), data);
|
49
|
+
yield fs.outputJson(filePath, newData, { spaces: 2 });
|
50
|
+
});
|
51
|
+
}
|
52
|
+
exports.writeJson = writeJson;
|
53
|
+
//# sourceMappingURL=writeJson.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"writeJson.js","sourceRoot":"","sources":["../../src/utils/writeJson.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAA+B;AAC/B,2CAA6B;AAa7B,SAAsB,SAAS,CAAC,IAAmB;;QACjD,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,GAAG,WAAW,EAAE,GAAG,IAAI,CAAC;QAEzD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAGhD,MAAM,UAAU,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QAEjD,IAAI,WAAW,GAA4B,EAAE,CAAC;QAG9C,IAAI,UAAU,EAAE;YACd,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;YACnE,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SACnC;QAGD,MAAM,OAAO,mCAAQ,WAAW,GAAK,IAAI,CAAE,CAAC;QAG5C,MAAM,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IACxD,CAAC;CAAA;AArBD,8BAqBC","sourcesContent":["import * as fs from 'fs-extra';\nimport * as path from 'path';\n\ninterface WriteJsonArgs {\n directory: string;\n fileName?: string;\n data: Record<string, any>;\n}\n\n/**\n * Writes data to a JSON file, creating the file if it doesn't exist.\n *\n * @param args - An object containing the directory where the JSON file is located, the file name (optional, defaults to 'meta.json'), and the data to be written to the file.\n */\nexport async function writeJson(args: WriteJsonArgs): Promise<void> {\n const { directory, data, fileName = 'meta.json' } = args;\n\n const filePath = path.join(directory, fileName);\n\n // Check if the file exists\n const fileExists = await fs.pathExists(filePath);\n\n let currentData: Record<string, unknown> = {};\n\n // If the file exists, read the current data\n if (fileExists) {\n const rawData = await fs.readFile(filePath, { encoding: 'utf-8' });\n currentData = JSON.parse(rawData);\n }\n\n // Merge the current data with the new data\n const newData = { ...currentData, ...data };\n\n // Write the new data to the file\n await fs.outputJson(filePath, newData, { spaces: 2 });\n}\n"]}
|