@devbro/pashmak 0.1.6 → 0.1.7

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.
@@ -64,10 +64,7 @@ class CreateProjectCommand extends Command {
64
64
  recursive: true
65
65
  });
66
66
  console.log(`Created project directory at: ${projectPath}`);
67
- let dirname = __dirname;
68
- if (!dirname) {
69
- dirname = path.dirname(fileURLToPath(import.meta.url));
70
- }
67
+ let dirname = typeof __dirname === "undefined" ? path.dirname(fileURLToPath(import.meta.url)) : __dirname;
71
68
  let basePath = path.join(dirname, `./base_project`);
72
69
  if (await this.folderExists(basePath) === false) {
73
70
  basePath = path.join(dirname, `../app/console/project/base_project`);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/app/console/project/CreateProjectCommand.ts"],"sourcesContent":["import { Command, Option } from \"clipanion\";\nimport { Case } from \"change-case-all\";\nimport path from \"path\";\nimport * as fs from \"fs/promises\";\nimport { fileURLToPath } from \"url\";\nimport handlebars from \"handlebars\";\nimport { execSync } from \"child_process\";\n\nexport class CreateProjectCommand extends Command {\n static paths = [[`create`, `project`]];\n\n static usage = Command.Usage({\n category: `Project`,\n description: `Create a new project`,\n details: `\n This command creates a new project with the specified name at the given path.\n If no path is provided, the project will be created in the current directory.\n `,\n examples: [\n [\n `Create a new project in specified directory`,\n `create project --path /path/to/my-project --git`,\n ],\n [\n `Create a new project at a specific path with git initialized`,\n `create project --path /path/to/my-project --git`,\n ],\n ],\n });\n\n projectPath = Option.String(\"--path\", { required: true });\n\n git = Option.Boolean(`--git`, false, {\n description: `Initialize a git repository in the new project`,\n });\n\n async folderExists(folderPath: string): Promise<boolean> {\n try {\n const stats = await fs.stat(folderPath);\n return stats.isDirectory();\n } catch (error: any) {\n if (error.code === \"ENOENT\") {\n return false; // Folder does not exist\n }\n throw error; // Other errors (e.g., permission issues)\n }\n }\n\n async execute() {\n // Create the project directory path by joining the specified path and project name\n const projectPath = path.join(this.projectPath);\n // Check if directory already exists\n try {\n await fs.access(projectPath);\n console.error(`Error: Directory ${projectPath} already exists.`);\n return 1;\n } catch {\n // Directory doesn't exist, we can proceed\n }\n\n await fs.mkdir(projectPath, { recursive: true });\n console.log(`Created project directory at: ${projectPath}`);\n\n let dirname = __dirname;\n if (!dirname) {\n dirname = path.dirname(fileURLToPath(import.meta.url));\n }\n\n let basePath = path.join(dirname, `./base_project`);\n if ((await this.folderExists(basePath)) === false) {\n // we are running a compiled code that was bundled and the code is running from ./dist/bin/ folder.\n basePath = path.join(dirname, `../app/console/project/base_project`);\n }\n\n console.log(`Using base project path: ${basePath}`);\n //copy content of ./base_project to the new project directory\n const baseProjectPath = basePath;\n\n await this.processTplFolder(baseProjectPath, projectPath, {});\n console.log(`Copied base project files to: ${projectPath}`);\n\n //modify package.json with foldername\n const packageJsonPath = path.join(projectPath, `package.json`);\n const packageJson = JSON.parse(await fs.readFile(packageJsonPath, `utf-8`));\n packageJson.name = Case.snake(path.basename(projectPath));\n await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));\n console.log(`Updated package.json with project name: ${packageJson.name}`);\n\n if (this.git) {\n try {\n execSync(\n 'git init; git add --all; git commit --allow-empty -m \"chore: first commit for pashmak\"',\n {\n cwd: projectPath,\n },\n );\n } catch (error) {\n console.error(`Failed to create project.`, error);\n return 1;\n }\n }\n }\n\n async processTplFolder(src: string, dest: string, data: any = {}) {\n const files = await fs.readdir(src, { withFileTypes: true });\n for (const file of files) {\n const srcPath = path.join(src, file.name);\n const destPath =\n file.isFile() && file.name.endsWith(\".tpl\")\n ? path.join(dest, file.name.substring(0, file.name.length - 4))\n : path.join(dest, file.name);\n\n if (file.isDirectory()) {\n await fs.mkdir(destPath, { recursive: true });\n await this.processTplFolder(srcPath, destPath, data);\n } else if (file.name.endsWith(\".tpl\")) {\n await this.processTplFile(srcPath, destPath, {});\n } else {\n throw new Error(\n \"unexpected non tpl file: \" + srcPath + \" \" + file.name,\n );\n }\n }\n }\n\n async processTplFile(src: string, dest: string, data: any = {}) {\n const compiledTemplate = handlebars.compile(\n (await fs.readFile(src)).toString(),\n );\n const template = await compiledTemplate(data);\n await fs.writeFile(dest, template);\n }\n}\n"],"mappings":";;AAAA,SAASA,SAASC,cAAc;AAChC,SAASC,YAAY;AACrB,OAAOC,UAAU;AACjB,YAAYC,QAAQ;AACpB,SAASC,qBAAqB;AAC9B,OAAOC,gBAAgB;AACvB,SAASC,gBAAgB;AAElB,MAAMC,6BAA6BR,QAAAA;EAR1C,OAQ0CA;;;EACxC,OAAOS,QAAQ;IAAC;MAAC;MAAU;;;EAE3B,OAAOC,QAAQV,QAAQW,MAAM;IAC3BC,UAAU;IACVC,aAAa;IACbC,SAAS;;;;IAITC,UAAU;MACR;QACE;QACA;;MAEF;QACE;QACA;;;EAGN,CAAA;EAEAC,cAAcf,OAAOgB,OAAO,UAAU;IAAEC,UAAU;EAAK,CAAA;EAEvDC,MAAMlB,OAAOmB,QAAQ,SAAS,OAAO;IACnCP,aAAa;EACf,CAAA;EAEA,MAAMQ,aAAaC,YAAsC;AACvD,QAAI;AACF,YAAMC,QAAQ,MAAMnB,GAAGoB,KAAKF,UAAAA;AAC5B,aAAOC,MAAME,YAAW;IAC1B,SAASC,OAAY;AACnB,UAAIA,MAAMC,SAAS,UAAU;AAC3B,eAAO;MACT;AACA,YAAMD;IACR;EACF;EAEA,MAAME,UAAU;AAEd,UAAMZ,cAAcb,KAAK0B,KAAK,KAAKb,WAAW;AAE9C,QAAI;AACF,YAAMZ,GAAG0B,OAAOd,WAAAA;AAChBe,cAAQL,MAAM,oBAAoBV,WAAAA,kBAA6B;AAC/D,aAAO;IACT,QAAQ;IAER;AAEA,UAAMZ,GAAG4B,MAAMhB,aAAa;MAAEiB,WAAW;IAAK,CAAA;AAC9CF,YAAQG,IAAI,iCAAiClB,WAAAA,EAAa;AAE1D,QAAImB,UAAUC;AACd,QAAI,CAACD,SAAS;AACZA,gBAAUhC,KAAKgC,QAAQ9B,cAAc,YAAYgC,GAAG,CAA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D7D,KAAK0B,KAAKwB,MAAMK,KAAKZ,IAAI;AAE/B,UAAIY,KAAKjC,YAAW,GAAI;AACtB,cAAMrB,GAAG4B,MAAM4B,UAAU;UAAE3B,WAAW;QAAK,CAAA;AAC3C,cAAM,KAAKO,iBAAiBmB,SAASC,UAAUN,IAAAA;MACjD,WAAWI,KAAKZ,KAAKgB,SAAS,MAAA,GAAS;AACrC,cAAM,KAAKG,eAAeN,SAASC,UAAU,CAAC,CAAA;MAChD,OAAO;AACL,cAAM,IAAIM,MACR,8BAA8BP,UAAU,MAAMD,KAAKZ,IAAI;MAE3D;IACF;EACF;EAEA,MAAMmB,eAAeb,KAAaC,MAAcC,OAAY,CAAC,GAAG;AAC9D,UAAMa,mBAAmB7D,WAAW8D,SACjC,MAAMhE,GAAGyC,SAASO,GAAAA,GAAMiB,SAAQ,CAAA;AAEnC,UAAMC,WAAW,MAAMH,iBAAiBb,IAAAA;AACxC,UAAMlD,GAAG6C,UAAUI,MAAMiB,QAAAA;EAC3B;AACF;","names":["Command","Option","Case","path","fs","fileURLToPath","handlebars","execSync","CreateProjectCommand","paths","usage","Usage","category","description","details","examples","projectPath","String","required","git","Boolean","folderExists","folderPath","stats","stat","isDirectory","error","code","execute","join","access","console","mkdir","recursive","log","dirname","__dirname","url","basePath","baseProjectPath","processTplFolder","packageJsonPath","packageJson","JSON","parse","readFile","name","snake","basename","writeFile","stringify","cwd","src","dest","data","files","readdir","withFileTypes","file","srcPath","destPath","isFile","endsWith","substring","length","processTplFile","Error","compiledTemplate","compile","toString","template"]}
1
+ {"version":3,"sources":["../../../../src/app/console/project/CreateProjectCommand.ts"],"sourcesContent":["import { Command, Option } from \"clipanion\";\nimport { Case } from \"change-case-all\";\nimport path from \"path\";\nimport * as fs from \"fs/promises\";\nimport { fileURLToPath } from \"url\";\nimport handlebars from \"handlebars\";\nimport { execSync } from \"child_process\";\n\nexport class CreateProjectCommand extends Command {\n static paths = [[`create`, `project`]];\n\n static usage = Command.Usage({\n category: `Project`,\n description: `Create a new project`,\n details: `\n This command creates a new project with the specified name at the given path.\n If no path is provided, the project will be created in the current directory.\n `,\n examples: [\n [\n `Create a new project in specified directory`,\n `create project --path /path/to/my-project --git`,\n ],\n [\n `Create a new project at a specific path with git initialized`,\n `create project --path /path/to/my-project --git`,\n ],\n ],\n });\n\n projectPath = Option.String(\"--path\", { required: true });\n\n git = Option.Boolean(`--git`, false, {\n description: `Initialize a git repository in the new project`,\n });\n\n async folderExists(folderPath: string): Promise<boolean> {\n try {\n const stats = await fs.stat(folderPath);\n return stats.isDirectory();\n } catch (error: any) {\n if (error.code === \"ENOENT\") {\n return false; // Folder does not exist\n }\n throw error; // Other errors (e.g., permission issues)\n }\n }\n\n async execute() {\n // Create the project directory path by joining the specified path and project name\n const projectPath = path.join(this.projectPath);\n // Check if directory already exists\n try {\n await fs.access(projectPath);\n console.error(`Error: Directory ${projectPath} already exists.`);\n return 1;\n } catch {\n // Directory doesn't exist, we can proceed\n }\n\n await fs.mkdir(projectPath, { recursive: true });\n console.log(`Created project directory at: ${projectPath}`);\n\n let dirname =\n typeof __dirname === \"undefined\"\n ? path.dirname(fileURLToPath(import.meta.url))\n : __dirname;\n\n let basePath = path.join(dirname, `./base_project`);\n if ((await this.folderExists(basePath)) === false) {\n // we are running a compiled code that was bundled and the code is running from ./dist/bin/ folder.\n basePath = path.join(dirname, `../app/console/project/base_project`);\n }\n\n console.log(`Using base project path: ${basePath}`);\n //copy content of ./base_project to the new project directory\n const baseProjectPath = basePath;\n\n await this.processTplFolder(baseProjectPath, projectPath, {});\n console.log(`Copied base project files to: ${projectPath}`);\n\n //modify package.json with foldername\n const packageJsonPath = path.join(projectPath, `package.json`);\n const packageJson = JSON.parse(await fs.readFile(packageJsonPath, `utf-8`));\n packageJson.name = Case.snake(path.basename(projectPath));\n await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));\n console.log(`Updated package.json with project name: ${packageJson.name}`);\n\n if (this.git) {\n try {\n execSync(\n 'git init; git add --all; git commit --allow-empty -m \"chore: first commit for pashmak\"',\n {\n cwd: projectPath,\n },\n );\n } catch (error) {\n console.error(`Failed to create project.`, error);\n return 1;\n }\n }\n }\n\n async processTplFolder(src: string, dest: string, data: any = {}) {\n const files = await fs.readdir(src, { withFileTypes: true });\n for (const file of files) {\n const srcPath = path.join(src, file.name);\n const destPath =\n file.isFile() && file.name.endsWith(\".tpl\")\n ? path.join(dest, file.name.substring(0, file.name.length - 4))\n : path.join(dest, file.name);\n\n if (file.isDirectory()) {\n await fs.mkdir(destPath, { recursive: true });\n await this.processTplFolder(srcPath, destPath, data);\n } else if (file.name.endsWith(\".tpl\")) {\n await this.processTplFile(srcPath, destPath, {});\n } else {\n throw new Error(\n \"unexpected non tpl file: \" + srcPath + \" \" + file.name,\n );\n }\n }\n }\n\n async processTplFile(src: string, dest: string, data: any = {}) {\n const compiledTemplate = handlebars.compile(\n (await fs.readFile(src)).toString(),\n );\n const template = await compiledTemplate(data);\n await fs.writeFile(dest, template);\n }\n}\n"],"mappings":";;AAAA,SAASA,SAASC,cAAc;AAChC,SAASC,YAAY;AACrB,OAAOC,UAAU;AACjB,YAAYC,QAAQ;AACpB,SAASC,qBAAqB;AAC9B,OAAOC,gBAAgB;AACvB,SAASC,gBAAgB;AAElB,MAAMC,6BAA6BR,QAAAA;EAR1C,OAQ0CA;;;EACxC,OAAOS,QAAQ;IAAC;MAAC;MAAU;;;EAE3B,OAAOC,QAAQV,QAAQW,MAAM;IAC3BC,UAAU;IACVC,aAAa;IACbC,SAAS;;;;IAITC,UAAU;MACR;QACE;QACA;;MAEF;QACE;QACA;;;EAGN,CAAA;EAEAC,cAAcf,OAAOgB,OAAO,UAAU;IAAEC,UAAU;EAAK,CAAA;EAEvDC,MAAMlB,OAAOmB,QAAQ,SAAS,OAAO;IACnCP,aAAa;EACf,CAAA;EAEA,MAAMQ,aAAaC,YAAsC;AACvD,QAAI;AACF,YAAMC,QAAQ,MAAMnB,GAAGoB,KAAKF,UAAAA;AAC5B,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C,KAAKnC;QACP,CAAA;MAEJ,SAASU,OAAO;AACdK,gBAAQL,MAAM,6BAA6BA,KAAAA;AAC3C,eAAO;MACT;IACF;EACF;EAEA,MAAMc,iBAAiBY,KAAaC,MAAcC,OAAY,CAAC,GAAG;AAChE,UAAMC,QAAQ,MAAMnD,GAAGoD,QAAQJ,KAAK;MAAEK,eAAe;IAAK,CAAA;AAC1D,eAAWC,QAAQH,OAAO;AACxB,YAAMI,UAAUxD,KAAK0B,KAAKuB,KAAKM,KAAKZ,IAAI;AACxC,YAAMc,WACJF,KAAKG,OAAM,KAAMH,KAAKZ,KAAKgB,SAAS,MAAA,IAChC3D,KAAK0B,KAAKwB,MAAMK,KAAKZ,KAAKiB,UAAU,GAAGL,KAAKZ,KAAKkB,SAAS,CAAA,CAAA,IAC1D7D,KAAK0B,KAAKwB,MAAMK,KAAKZ,IAAI;AAE/B,UAAIY,KAAKjC,YAAW,GAAI;AACtB,cAAMrB,GAAG4B,MAAM4B,UAAU;UAAE3B,WAAW;QAAK,CAAA;AAC3C,cAAM,KAAKO,iBAAiBmB,SAASC,UAAUN,IAAAA;MACjD,WAAWI,KAAKZ,KAAKgB,SAAS,MAAA,GAAS;AACrC,cAAM,KAAKG,eAAeN,SAASC,UAAU,CAAC,CAAA;MAChD,OAAO;AACL,cAAM,IAAIM,MACR,8BAA8BP,UAAU,MAAMD,KAAKZ,IAAI;MAE3D;IACF;EACF;EAEA,MAAMmB,eAAeb,KAAaC,MAAcC,OAAY,CAAC,GAAG;AAC9D,UAAMa,mBAAmB7D,WAAW8D,SACjC,MAAMhE,GAAGyC,SAASO,GAAAA,GAAMiB,SAAQ,CAAA;AAEnC,UAAMC,WAAW,MAAMH,iBAAiBb,IAAAA;AACxC,UAAMlD,GAAG6C,UAAUI,MAAMiB,QAAAA;EAC3B;AACF;","names":["Command","Option","Case","path","fs","fileURLToPath","handlebars","execSync","CreateProjectCommand","paths","usage","Usage","category","description","details","examples","projectPath","String","required","git","Boolean","folderExists","folderPath","stats","stat","isDirectory","error","code","execute","join","access","console","mkdir","recursive","log","dirname","__dirname","url","basePath","baseProjectPath","processTplFolder","packageJsonPath","packageJson","JSON","parse","readFile","name","snake","basename","writeFile","stringify","cwd","src","dest","data","files","readdir","withFileTypes","file","srcPath","destPath","isFile","endsWith","substring","length","processTplFile","Error","compiledTemplate","compile","toString","template"]}
@@ -93,10 +93,7 @@ var CreateProjectCommand = class extends import_clipanion.Command {
93
93
  recursive: true
94
94
  });
95
95
  console.log(`Created project directory at: ${projectPath}`);
96
- let dirname = __dirname;
97
- if (!dirname) {
98
- dirname = import_path.default.dirname((0, import_url.fileURLToPath)(import_meta.url));
99
- }
96
+ let dirname = typeof __dirname === "undefined" ? import_path.default.dirname((0, import_url.fileURLToPath)(import_meta.url)) : __dirname;
100
97
  let basePath = import_path.default.join(dirname, `./base_project`);
101
98
  if (await this.folderExists(basePath) === false) {
102
99
  basePath = import_path.default.join(dirname, `../app/console/project/base_project`);
@@ -0,0 +1,8 @@
1
+ import { Request, Response } from '@devbro/neko-router';
2
+
3
+ declare function cors(options?: {
4
+ allowedOrigins?: (string | RegExp)[];
5
+ }): (req: Request, res: Response, next: () => Promise<void>) => Promise<void>;
6
+ declare function dbTransaction(req: Request, res: Response, next: () => Promise<void>): Promise<void>;
7
+
8
+ export { cors, dbTransaction };
@@ -0,0 +1,41 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ import { db } from "./facades";
4
+ function cors(options = {}) {
5
+ return async (req, res, next) => {
6
+ const allowedOrigins = options.allowedOrigins || [
7
+ "*"
8
+ ];
9
+ const origin = req.headers.origin || "*";
10
+ for (const allowedOrigin of allowedOrigins) {
11
+ if (typeof allowedOrigin === "string" && allowedOrigin === origin) {
12
+ res.setHeader("Access-Control-Allow-Origin", allowedOrigin);
13
+ break;
14
+ } else if (allowedOrigin instanceof RegExp && allowedOrigin.test(origin)) {
15
+ res.setHeader("Access-Control-Allow-Origin", origin);
16
+ break;
17
+ } else if (allowedOrigin === "*") {
18
+ res.setHeader("Access-Control-Allow-Origin", "*");
19
+ break;
20
+ }
21
+ }
22
+ res.setHeader("Access-Control-Allow-Headers", "*");
23
+ await next();
24
+ };
25
+ }
26
+ __name(cors, "cors");
27
+ async function dbTransaction(req, res, next) {
28
+ try {
29
+ await db().beginTransaction();
30
+ await next();
31
+ await db().commit();
32
+ } finally {
33
+ await db().rollback();
34
+ }
35
+ }
36
+ __name(dbTransaction, "dbTransaction");
37
+ export {
38
+ cors,
39
+ dbTransaction
40
+ };
41
+ //# sourceMappingURL=middlewares.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/middlewares.ts"],"sourcesContent":["import { Request, Response } from \"@devbro/neko-router\";\nimport { logger, db } from \"./facades\";\n\nexport function cors(\n options: { allowedOrigins?: (string | RegExp)[] } = {},\n): (req: Request, res: Response, next: () => Promise<void>) => Promise<void> {\n return async (\n req: Request,\n res: Response,\n next: () => Promise<void>,\n ): Promise<void> => {\n const allowedOrigins = options.allowedOrigins || [\"*\"];\n const origin = req.headers.origin || \"*\";\n\n for (const allowedOrigin of allowedOrigins) {\n if (typeof allowedOrigin === \"string\" && allowedOrigin === origin) {\n res.setHeader(\"Access-Control-Allow-Origin\", allowedOrigin);\n break;\n } else if (\n allowedOrigin instanceof RegExp &&\n allowedOrigin.test(origin)\n ) {\n res.setHeader(\"Access-Control-Allow-Origin\", origin);\n break;\n } else if (allowedOrigin === \"*\") {\n res.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n break;\n }\n }\n\n res.setHeader(\"Access-Control-Allow-Headers\", \"*\");\n await next();\n };\n}\n\nexport async function dbTransaction(\n req: Request,\n res: Response,\n next: () => Promise<void>,\n): Promise<void> {\n try {\n await db().beginTransaction();\n await next();\n await db().commit();\n } finally {\n await db().rollback();\n }\n}\n"],"mappings":";;AACA,SAAiBA,UAAU;AAEpB,SAASC,KACdC,UAAoD,CAAC,GAAC;AAEtD,SAAO,OACLC,KACAC,KACAC,SAAAA;AAEA,UAAMC,iBAAiBJ,QAAQI,kBAAkB;MAAC;;AAClD,UAAMC,SAASJ,IAAIK,QAAQD,UAAU;AAErC,eAAWE,iBAAiBH,gBAAgB;AAC1C,UAAI,OAAOG,kBAAkB,YAAYA,kBAAkBF,QAAQ;AACjEH,YAAIM,UAAU,+BAA+BD,aAAAA;AAC7C;MACF,WACEA,yBAAyBE,UACzBF,cAAcG,KAAKL,MAAAA,GACnB;AACAH,YAAIM,UAAU,+BAA+BH,MAAAA;AAC7C;MACF,WAAWE,kBAAkB,KAAK;AAChCL,YAAIM,UAAU,+BAA+B,GAAA;AAC7C;MACF;IACF;AAEAN,QAAIM,UAAU,gCAAgC,GAAA;AAC9C,UAAML,KAAAA;EACR;AACF;AA9BgBJ;AAgChB,eAAsBY,cACpBV,KACAC,KACAC,MAAyB;AAEzB,MAAI;AACF,UAAML,GAAAA,EAAKc,iBAAgB;AAC3B,UAAMT,KAAAA;AACN,UAAML,GAAAA,EAAKe,OAAM;EACnB,UAAA;AACE,UAAMf,GAAAA,EAAKgB,SAAQ;EACrB;AACF;AAZsBH;","names":["db","cors","options","req","res","next","allowedOrigins","origin","headers","allowedOrigin","setHeader","RegExp","test","dbTransaction","beginTransaction","commit","rollback"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devbro/pashmak",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "testing application for the entire repo",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -73,6 +73,11 @@
73
73
  "import": "./dist/mailer.js",
74
74
  "require": "./dist/mailer.js"
75
75
  },
76
+ "./middlewares": {
77
+ "types": "./dist/middlewares.d.ts",
78
+ "import": "./dist/middlewares.js",
79
+ "require": "./dist/middlewares.js"
80
+ },
76
81
  "./package.json": "./package.json"
77
82
  },
78
83
  "scripts": {