@p8ec/shared 1.1.6 → 1.2.0

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # P(8) Global Shared Library for Node.js and TypeScript/JavaScript
1
+ # P(8) Shared Libraries for Node.js
2
2
 
3
- This repository contains the shared library for all P(8) products.
3
+ Shared TypeScript/JavaScript libraries for P(8) products.
4
4
 
5
- ## Javascript Shared Configuration
5
+ ## Shared Configuration
6
6
 
7
- This repository contains the shared configuration for all P(8) products.
7
+ Shared configuration for P(8) projects.
8
8
 
9
9
  ### Usage
10
10
 
@@ -17,10 +17,11 @@ npm i -D @p8ec/shared
17
17
  #### Initialization
18
18
 
19
19
  ```shell
20
- p8-shared-cli init
20
+ p8-cli init
21
21
  ```
22
22
 
23
- This command will create the following files in your project and remove corresponding configuration entries from `package.json`:
23
+ This command will create the following files in your project and remove corresponding configuration entries
24
+ from `package.json`:
24
25
 
25
26
  #### **`.eslintrc.js`**
26
27
 
@@ -33,3 +34,18 @@ module.exports = require('@p8ec/shared').eslintConfigRecommended;
33
34
  ```javascript
34
35
  module.exports = require('@p8ec/shared').prettierConfigRecommended;
35
36
  ```
37
+
38
+ ## P(8) CLI Tool
39
+
40
+ ### Syntax
41
+
42
+ ```shell
43
+ p8-cli [command] [options]
44
+ ```
45
+
46
+ ### Commands
47
+
48
+ - `init` - Initialize P(8) shared configuration in your project.
49
+ - `dirn` - Get the directory name. Options: `0` - current directory (default), `1` - parent directory, `2` - 2 levels up
50
+ directory, etc.
51
+
@@ -47,9 +47,10 @@ const fs = __importStar(require("fs"));
47
47
  const ferramenta_1 = require("ferramenta");
48
48
  const args = ferramenta_1.processArgs.args;
49
49
  const self = path.parse(ferramenta_1.processArgs.name).name;
50
+ const writeLn = console.log;
51
+ const writeFile = (name, data) => fs.writeFileSync(path.join(process.cwd(), name), data);
50
52
  if (args.length === 0) {
51
- // eslint-disable-next-line no-console
52
- console.log(`
53
+ writeLn(`
53
54
  Usage: ${self} [command] [options]
54
55
 
55
56
  Commands:
@@ -60,25 +61,51 @@ Commands:
60
61
  `);
61
62
  process.exit(1);
62
63
  }
64
+ /**
65
+ * Initializes a TypeScript project with P8 shared configurations.
66
+ */
63
67
  const init = () => {
64
- // eslint-disable-next-line no-console
65
- console.log('Creating .eslintrc.js...');
66
- fs.writeFileSync(path.join(process.cwd(), '.eslintrc.js'), `module.exports = require('@p8ec/shared').eslintConfigRecommended;`);
67
- // eslint-disable-next-line no-console
68
- console.log('Creating .prettierrc.js...');
69
- fs.writeFileSync(path.join(process.cwd(), '.prettierrc.js'), `module.exports = require('@p8ec/shared').prettierConfigRecommended;`);
70
- // Remove eslintConfig and prettier from package.json
71
- // eslint-disable-next-line no-console
72
- console.log('Removing eslintConfig and prettier from package.json...');
68
+ writeLn('Creating .eslintrc.js...');
69
+ writeFile('.eslintrc.js', `module.exports = require('@p8ec/shared').eslintConfigRecommended;`);
70
+ writeLn('Creating .prettierrc.js...');
71
+ writeFile('.prettierrc.js', `module.exports = require('@p8ec/shared').prettierConfigRecommended;`);
72
+ // Cleanup package.json
73
+ writeLn('Removing eslintConfig and prettier from package.json...');
73
74
  const packageJson = JSON.parse(String(fs.readFileSync(path.join(process.cwd(), 'package.json'))));
74
- delete packageJson['eslintConfig'];
75
- delete packageJson['prettier'];
76
- fs.writeFileSync(path.join(process.cwd(), 'package.json'), JSON.stringify(packageJson, null, 2));
75
+ if (packageJson['eslintConfig']) {
76
+ writeLn('Backing up eslintConfig to eslint.package.json.bak...');
77
+ writeFile('eslint.package.json.bak', packageJson['eslintConfig']);
78
+ delete packageJson['eslintConfig'];
79
+ }
80
+ if (packageJson['prettier']) {
81
+ writeLn('Backing up prettier to prettier.package.json.bak...');
82
+ writeFile('prettier.package.json.bak', packageJson['prettier']);
83
+ delete packageJson['prettier'];
84
+ }
85
+ writeFile('package.json', JSON.stringify(packageJson, null, 2));
86
+ };
87
+ /**
88
+ * Returns the directory name of the caller, optionally returns a directory name specified levels up.
89
+ */
90
+ const dirn = (levelsUp) => {
91
+ const DEFAULT_LEVELS_UP = 0;
92
+ levelsUp !== null && levelsUp !== void 0 ? levelsUp : (levelsUp = `${DEFAULT_LEVELS_UP}`);
93
+ const levels = parseInt(levelsUp) || DEFAULT_LEVELS_UP;
94
+ return process.cwd().split(path.sep).reverse()[levels];
95
+ };
96
+ /**
97
+ * Returns the caller PID
98
+ */
99
+ const pid = () => {
100
+ return process.pid;
77
101
  };
78
102
  switch (args[0]) {
79
103
  case 'init':
80
104
  init();
81
105
  break;
106
+ case 'dirn':
107
+ writeLn(dirn(args[1]));
108
+ break;
82
109
  default:
83
110
  // eslint-disable-next-line no-console
84
111
  console.error(`Unknown command: ${args[0]}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@p8ec/shared",
3
- "version": "1.1.6",
3
+ "version": "1.2.0",
4
4
  "description": "P(8) Global Shared Library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,8 +15,9 @@
15
15
  "build:esm": "tsc -p configs/tsconfig.esm.json",
16
16
  "build:cjs": "tsc -p configs/tsconfig.cjs.json && chmod -R +x dist/cjs/bin/*.js",
17
17
  "build:types": "tsc -p configs/tsconfig.types.json",
18
- "clean": "rimraf dist docs coverage",
19
- "reset": "npm run clean && rimraf node_modules && npm install",
18
+ "clean": "rm -rf dist docs coverage",
19
+ "cli": "ts-node src/bin/p8-shared-cli.ts",
20
+ "reset": "npm run clean && rm -rf node_modules && npm install",
20
21
  "prepare": "husky install",
21
22
  "test": "jest --no-cache --runInBand",
22
23
  "test:cov": "jest --coverage --no-cache --runInBand",
@@ -27,22 +28,23 @@
27
28
  "docs:clean": "rimraf docs"
28
29
  },
29
30
  "bin": {
30
- "p8-shared-cli": "dist/cjs/bin/p8-shared-cli.js"
31
+ "p8-shared-cli": "dist/cjs/bin/p8-shared-cli.js",
32
+ "p8-cli": "dist/cjs/bin/p8-shared-cli.js"
31
33
  },
32
34
  "main": "dist/cjs/index.js",
33
35
  "module": "dist/esm/index.js",
34
36
  "types": "dist/types/index.d.ts",
35
37
  "devDependencies": {
36
- "@commitlint/cli": "^19.2.2",
38
+ "@commitlint/cli": "^19.3.0",
37
39
  "@commitlint/config-conventional": "^19.2.2",
38
40
  "@swc/cli": "^0.3.12",
39
- "@swc/core": "^1.4.16",
41
+ "@swc/core": "^1.5.27",
40
42
  "@swc/jest": "^0.2.36",
41
- "@types/jest": "^29.5.11",
43
+ "@types/jest": "^29.5.12",
42
44
  "husky": "^9.0.11",
43
45
  "jest": "^29.7.0",
44
- "lint-staged": "^15.2.0",
45
- "rimraf": "^5.0.5",
46
+ "lint-staged": "^15.2.5",
47
+ "rimraf": "^5.0.7",
46
48
  "ts-node": "^10.9.2",
47
49
  "typedoc": "^0.25.13",
48
50
  "typescript": "^5.4.5"
@@ -117,13 +119,13 @@
117
119
  "*.{js,jsx,ts,tsx}": "npm run lint:fix"
118
120
  },
119
121
  "peerDependencies": {
120
- "ferramenta": "^1.3.0",
121
- "@typescript-eslint/eslint-plugin": "^7.7.0",
122
+ "@typescript-eslint/eslint-plugin": "^7",
122
123
  "@typescript-eslint/parser": "^7.7.0",
123
- "eslint": "^8.57.0",
124
- "eslint-config-prettier": "^9.1.0",
125
- "eslint-plugin-header": "^3.1.1",
126
- "eslint-plugin-prettier": "^5.1.0",
127
- "prettier": "3.2.5"
124
+ "eslint": "^8 || ^9",
125
+ "eslint-config-prettier": "^9",
126
+ "eslint-plugin-header": "^3",
127
+ "eslint-plugin-prettier": "^5",
128
+ "ferramenta": "^1.3.0",
129
+ "prettier": "^3.3.3"
128
130
  }
129
131
  }