瀏覽代碼

Initial commit

master
Dylan Baker 5 年之前
當前提交
2f6045e6f9
共有 7 個文件被更改,包括 173 次插入0 次删除
  1. 7
    0
      LICENSE
  2. 48
    0
      README.md
  3. 22
    0
      lib.js
  4. 5
    0
      package-lock.json
  5. 13
    0
      package.json
  6. 47
    0
      pj.js
  7. 31
    0
      test.js

+ 7
- 0
LICENSE 查看文件

@@ -0,0 +1,7 @@
1
+Copyright 2018 Dylan Baker
2
+
3
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 48
- 0
README.md 查看文件

@@ -0,0 +1,48 @@
1
+# pj
2
+
3
+`pj` is a small command-line utility for inspecting the contents of
4
+`package.json` files.
5
+
6
+## Installation
7
+
8
+```console
9
+$ npm install -g pajamas-cli
10
+```
11
+
12
+## Usage
13
+
14
+Invoked without arguments in a directory containing a `package.json`, `pj` will
15
+simply print the entire object. To inspect a property of the object, pass an
16
+argument consisting of the path to the property using dot notation. For
17
+example:
18
+
19
+```console
20
+$ cat package.json
21
+{
22
+  "name": "My Package",
23
+  "version": "0.0.1",
24
+  "license": "MIT",
25
+  "keywords": ["node", "json", "cli"]
26
+  "scripts": {
27
+    "test": "node test.js"
28
+  }
29
+}
30
+$ pj version
31
+0.0.1
32
+$ pj scripts.test
33
+node test.js
34
+```
35
+
36
+`pj` also supports arrays, also using dot notation:
37
+
38
+```console
39
+$ pj keywords.0
40
+node
41
+$ pj keywords.1
42
+json
43
+```
44
+
45
+## License
46
+
47
+`pj` is open source software under the terms of the
48
+[MIT license] (http://opensource.org/licenses/MIT).

+ 22
- 0
lib.js 查看文件

@@ -0,0 +1,22 @@
1
+const traversePath = (object, path) => {
2
+  const [key, ...rest] = path;
3
+  if (key === undefined) return object;
4
+  if (object.hasOwnProperty(key)) {
5
+    return traversePath(object[key], rest);
6
+  }
7
+  return `Property '${key}' does not exist`;
8
+};
9
+
10
+const pj = (contents, path) => {
11
+  try {
12
+    const object = JSON.parse(contents);
13
+    return path.length === 0 ? object : traversePath(object, path);
14
+  } catch (e) {
15
+    return new Error('Not a valid JSON file');
16
+  }
17
+};
18
+
19
+module.exports = {
20
+  traversePath: traversePath,
21
+  pj: pj,
22
+};

+ 5
- 0
package-lock.json 查看文件

@@ -0,0 +1,5 @@
1
+{
2
+  "name": "pajama-cli",
3
+  "version": "0.1.0",
4
+  "lockfileVersion": 1
5
+}

+ 13
- 0
package.json 查看文件

@@ -0,0 +1,13 @@
1
+{
2
+  "name": "pajamas-cli",
3
+  "version": "0.1.0",
4
+  "description": "package.json inspector",
5
+  "bin": {
6
+    "pj": "pj.js"
7
+  },
8
+  "scripts": {
9
+    "test": "node test.js"
10
+  },
11
+  "author": "Dylan Baker <dylanbaker.ct@gmail.com>",
12
+  "license": "MIT"
13
+}

+ 47
- 0
pj.js 查看文件

@@ -0,0 +1,47 @@
1
+#!/usr/bin/env node
2
+
3
+const fs = require('fs');
4
+const pj = require('./lib').pj;
5
+
6
+if (process.argv[2] === '--help') {
7
+  console.log(`Usage: pj [path]
8
+
9
+A path is a string of the form object.property.subproperty representing the
10
+property to look up. For example, consider the following package.json file:
11
+
12
+{
13
+  "name": "My Package",
14
+  "version": "0.0.1",
15
+  "license": "MIT",
16
+  "keywords": ["node", "json", "cli"]
17
+  "scripts": {
18
+    "test": "node test.js"
19
+  }
20
+}
21
+
22
+You could run \`pj name\`, which would print "My Package", or
23
+\`pj scripts.test\` which would print "node test.js".`);
24
+  process.exit();
25
+}
26
+
27
+const path = process.argv.length < 3 ? [] : process.argv[2].split('.');
28
+
29
+fs.readFile('./package.json', 'utf-8', (err, contents) => {
30
+  if (err) {
31
+    switch (err.code) {
32
+      case 'ENOENT':
33
+        console.error('No package.json found in the current directory');
34
+        process.exit();
35
+      default:
36
+        throw err;
37
+    }
38
+  }
39
+  const result = pj(contents, path);
40
+  switch (result.constructor) {
41
+    case Error:
42
+      console.error(result.message);
43
+      break;
44
+    default:
45
+      console.log(result);
46
+  }
47
+});

+ 31
- 0
test.js 查看文件

@@ -0,0 +1,31 @@
1
+const assert = require('assert');
2
+const traversePath = require('./lib').traversePath;
3
+
4
+const ESCAPE_CODES = {
5
+  RESET_COLOR: '\x1b[0m',
6
+  FG_GREEN: '\x1b[32m',
7
+};
8
+
9
+const testCases = [
10
+  {
11
+    expected: 1,
12
+    actual: traversePath({ a: 1 }, ['a']),
13
+  },
14
+  {
15
+    expected: 3,
16
+    actual: traversePath({ a: { b: { c: 3 } } }, ['a', 'b', 'c']),
17
+  },
18
+  {
19
+    expected: 2,
20
+    actual: traversePath({ a: [1, 2, 3] }, ['a', 1]),
21
+  },
22
+];
23
+
24
+testCases.forEach((testCase, i) => {
25
+  assert.deepEqual(testCase.actual, testCase.expected);
26
+  [ESCAPE_CODES.FG_GREEN, '.', ESCAPE_CODES.RESET_COLOR].forEach(el =>
27
+    process.stdout.write(el)
28
+  );
29
+});
30
+
31
+console.log(ESCAPE_CODES.FG_GREEN, 'OK', ESCAPE_CODES.RESET_COLOR);

Loading…
取消
儲存