@kokiito0926/fs2xml 0.0.7 → 0.0.9

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.
Files changed (2) hide show
  1. package/index.js +30 -43
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,15 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // >> $ ./index.js "./example/example.txt"
4
- // >> $ ./index.js "./example/sub/example.js"
5
-
6
- // >> $ ./index.js "./example/**/*.txt"
7
- // >> $ ./index.js "./example/**/*.txt" --ignore "./example/ignore/**"
8
-
9
3
  import { fs, path, minimist, glob } from "zx";
10
- import { create } from "xmlbuilder2";
11
4
  import ignore from "ignore";
12
5
  import globParent from "glob-parent";
6
+ import xml2js from "xml2js";
13
7
 
14
8
  async function loadNearestGitignore(targetPattern) {
15
9
  const ig = ignore();
@@ -28,8 +22,6 @@ async function loadNearestGitignore(targetPattern) {
28
22
  const gitignorePath = path.join(currentDir, ".gitignore");
29
23
 
30
24
  if (fs.existsSync(gitignorePath)) {
31
- // console.log(gitignorePath);
32
-
33
25
  const content = await fs.readFile(gitignorePath, "utf8");
34
26
  ig.add(content);
35
27
 
@@ -53,7 +45,6 @@ const { ig, baseDir } =
53
45
  gitignore == false ? { ig: ignore(), baseDir: process.cwd() } : await loadNearestGitignore(target);
54
46
 
55
47
  const defaultIgnore = [];
56
- // const defaultIgnore = ["node_modules/**", ".git/**"];
57
48
 
58
49
  const userIgnore = args.ignore ? (Array.isArray(args.ignore) ? args.ignore : [args.ignore]) : [];
59
50
 
@@ -78,6 +69,9 @@ async function getFileData(filePath) {
78
69
  content = content.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "");
79
70
  if (!content) return null;
80
71
 
72
+ content = content.replace(/]]>/g, "]]]]><![CDATA[>");
73
+ if (!content) return null;
74
+
81
75
  return {
82
76
  name: path.basename(filePath),
83
77
  path: filePath.replace(/\\/g, "/"),
@@ -86,11 +80,17 @@ async function getFileData(filePath) {
86
80
  }
87
81
 
88
82
  if (files.length === 0) {
89
- process.stderr.write(`No files matched the pattern: ${pattern}\n`);
90
- process.exit(0);
83
+ process.exit(1);
91
84
  }
92
85
 
93
86
  let xmlOutput = "";
87
+ let xmlObject = {};
88
+
89
+ const builder = new xml2js.Builder({
90
+ cdata: true,
91
+ xmldec: { version: "1.0", encoding: "UTF-8" },
92
+ renderOpts: { pretty: true },
93
+ });
94
94
 
95
95
  if (files.length === 1) {
96
96
  const data = await getFileData(files[0]);
@@ -98,19 +98,13 @@ if (files.length === 1) {
98
98
  process.exit(1);
99
99
  }
100
100
 
101
- xmlOutput = create({ version: "1.0", encoding: "UTF-8" })
102
- .ele("file")
103
- .ele("name")
104
- .txt(data.name)
105
- .up()
106
- .ele("path")
107
- .txt(data.path)
108
- .up()
109
- .ele("content")
110
- // .txt(data.content)
111
- .dat(data.content)
112
- .up()
113
- .end({ prettyPrint: true });
101
+ xmlObject = {
102
+ file: {
103
+ name: data.name,
104
+ path: data.path,
105
+ content: data.content,
106
+ },
107
+ };
114
108
  } else {
115
109
  const allFiles = [];
116
110
  for (const file of files) {
@@ -122,24 +116,17 @@ if (files.length === 1) {
122
116
  if (!allFiles.length) {
123
117
  process.exit(1);
124
118
  }
125
- // console.log(allFiles);
126
-
127
- const root = create({ version: "1.0", encoding: "UTF-8" }).ele("files");
128
- for (const f of allFiles) {
129
- root.ele("file")
130
- .ele("name")
131
- .txt(f.name)
132
- .up()
133
- .ele("path")
134
- .txt(f.path)
135
- .up()
136
- .ele("content")
137
- // .txt(f.content)
138
- .dat(f.content)
139
- .up()
140
- .up();
141
- }
142
- xmlOutput = root.end({ prettyPrint: true });
119
+
120
+ xmlObject = {
121
+ files: {
122
+ file: allFiles.map((f) => ({
123
+ name: f.name,
124
+ path: f.path,
125
+ content: f.content,
126
+ })),
127
+ },
128
+ };
143
129
  }
144
130
 
131
+ xmlOutput = builder.buildObject(xmlObject);
145
132
  console.log(xmlOutput);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokiito0926/fs2xml",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "private": false,
5
5
  "description": "ファイルをXML形式でまとめることができるコマンドラインのツールです。",
6
6
  "keywords": [
@@ -36,7 +36,7 @@
36
36
  "dependencies": {
37
37
  "glob-parent": "^6.0.2",
38
38
  "ignore": "^7.0.5",
39
- "xmlbuilder2": "^4.0.3",
39
+ "xml2js": "^0.6.2",
40
40
  "zx": "^8.8.4"
41
41
  },
42
42
  "engines": {