saladplate 0.1.1-a → 0.1.1
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/saladplate.js +25 -30
- package/dist/src/index.js +16 -23
- package/package.json +1 -1
package/dist/bin/saladplate.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -9,18 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
9
|
});
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const path_1 = __importDefault(require("path"));
|
|
18
|
-
const process_1 = __importDefault(require("process"));
|
|
19
|
-
const util_1 = __importDefault(require("util"));
|
|
20
|
-
const index_1 = require("../src/index");
|
|
11
|
+
import fs from "fs";
|
|
12
|
+
import path from "path";
|
|
13
|
+
import process from "process";
|
|
14
|
+
import util from "util";
|
|
15
|
+
import { BIN, template } from "../src/index";
|
|
21
16
|
const parseArgsConfig = {
|
|
22
17
|
// Exclude the first two arguments: node and the script itself.
|
|
23
|
-
args:
|
|
18
|
+
args: process.argv.slice(2),
|
|
24
19
|
options: {
|
|
25
20
|
debug: {
|
|
26
21
|
type: "boolean",
|
|
@@ -58,7 +53,7 @@ const parseArgsConfig = {
|
|
|
58
53
|
allowPositionals: true,
|
|
59
54
|
};
|
|
60
55
|
function help() {
|
|
61
|
-
console.info(`Usage: ${
|
|
56
|
+
console.info(`Usage: ${BIN} [options] <file>...`);
|
|
62
57
|
console.info(`Options:`);
|
|
63
58
|
const entries = Object.entries(parseArgsConfig.options);
|
|
64
59
|
const prefixes = {};
|
|
@@ -76,11 +71,11 @@ function help() {
|
|
|
76
71
|
console.info("");
|
|
77
72
|
}
|
|
78
73
|
function version() {
|
|
79
|
-
const version =
|
|
80
|
-
console.info(`${
|
|
74
|
+
const version = process.env.npm_package_version;
|
|
75
|
+
console.info(`${BIN}: version ${version !== null && version !== void 0 ? version : "unknown"}`);
|
|
81
76
|
}
|
|
82
77
|
const parseArgs = () => {
|
|
83
|
-
const { values: options, positionals: filenames } =
|
|
78
|
+
const { values: options, positionals: filenames } = util.parseArgs(parseArgsConfig);
|
|
84
79
|
return { options, filenames };
|
|
85
80
|
};
|
|
86
81
|
let output = null;
|
|
@@ -91,14 +86,14 @@ function outputForFilename(filename, options) {
|
|
|
91
86
|
}
|
|
92
87
|
if (options.output) {
|
|
93
88
|
output = {
|
|
94
|
-
file: yield
|
|
89
|
+
file: yield fs.promises.open(options.output, "w"),
|
|
95
90
|
filename: options.output,
|
|
96
91
|
};
|
|
97
92
|
return output;
|
|
98
93
|
}
|
|
99
94
|
if (options.directory) {
|
|
100
|
-
const basename =
|
|
101
|
-
let outputFilename =
|
|
95
|
+
const basename = path.basename(filename);
|
|
96
|
+
let outputFilename = path.join(options.directory, basename);
|
|
102
97
|
if (options.suffix) {
|
|
103
98
|
outputFilename = outputFilename.replace(/\.[^.]+$/, options.suffix);
|
|
104
99
|
}
|
|
@@ -108,7 +103,7 @@ function outputForFilename(filename, options) {
|
|
|
108
103
|
};
|
|
109
104
|
}
|
|
110
105
|
output = {
|
|
111
|
-
file: yield
|
|
106
|
+
file: yield fs.promises.open("/dev/stdout", "w"),
|
|
112
107
|
filename: "/dev/stdout",
|
|
113
108
|
};
|
|
114
109
|
return output;
|
|
@@ -118,7 +113,7 @@ function main() {
|
|
|
118
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
119
114
|
const { options, filenames } = parseArgs();
|
|
120
115
|
if (options.debug) {
|
|
121
|
-
console.debug(`${
|
|
116
|
+
console.debug(`${BIN}: debug mode enabled`);
|
|
122
117
|
}
|
|
123
118
|
if (options.help) {
|
|
124
119
|
help();
|
|
@@ -129,7 +124,7 @@ function main() {
|
|
|
129
124
|
return;
|
|
130
125
|
}
|
|
131
126
|
if (filenames.length === 0) {
|
|
132
|
-
console.error(`${
|
|
127
|
+
console.error(`${BIN}: no input files; use -- for stdin`);
|
|
133
128
|
help();
|
|
134
129
|
return -1;
|
|
135
130
|
}
|
|
@@ -137,15 +132,15 @@ function main() {
|
|
|
137
132
|
if (filename === "--") {
|
|
138
133
|
filename = "/dev/stdin";
|
|
139
134
|
}
|
|
140
|
-
options.debug && console.debug(`${
|
|
141
|
-
const content = yield
|
|
135
|
+
options.debug && console.debug(`${BIN}: reading ${filename}...`);
|
|
136
|
+
const content = yield fs.promises.readFile(filename, {
|
|
142
137
|
encoding: "utf-8",
|
|
143
138
|
});
|
|
144
|
-
options.debug && console.debug(`${
|
|
145
|
-
const output = yield
|
|
139
|
+
options.debug && console.debug(`${BIN}: templating ${filename}...`);
|
|
140
|
+
const output = yield template(content, filename, options);
|
|
146
141
|
const { file: outputFile, filename: outputFilename } = yield outputForFilename(filename, options);
|
|
147
|
-
options.debug && console.debug(`${
|
|
148
|
-
yield
|
|
142
|
+
options.debug && console.debug(`${BIN}: writing ${outputFilename}...`);
|
|
143
|
+
yield fs.promises.writeFile(outputFile, output, {
|
|
149
144
|
encoding: "utf-8",
|
|
150
145
|
});
|
|
151
146
|
})));
|
|
@@ -153,9 +148,9 @@ function main() {
|
|
|
153
148
|
}
|
|
154
149
|
main()
|
|
155
150
|
.then((i) => {
|
|
156
|
-
|
|
151
|
+
process.exit(i !== null && i !== void 0 ? i : 0);
|
|
157
152
|
})
|
|
158
153
|
.catch((e) => {
|
|
159
|
-
console.error(`${
|
|
160
|
-
|
|
154
|
+
console.error(`${BIN}: unhandled error:`, e);
|
|
155
|
+
process.exit(-1);
|
|
161
156
|
});
|
package/dist/src/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,22 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const child_process_1 = require("child_process");
|
|
17
|
-
const fs_1 = __importDefault(require("fs"));
|
|
18
|
-
const path_1 = __importDefault(require("path"));
|
|
19
|
-
const process_1 = __importDefault(require("process"));
|
|
20
|
-
const util_1 = __importDefault(require("util"));
|
|
10
|
+
import { exec } from "child_process";
|
|
11
|
+
import fs from "fs";
|
|
12
|
+
import path from "path";
|
|
13
|
+
import process from "process";
|
|
14
|
+
import util from "util";
|
|
21
15
|
/**
|
|
22
16
|
* The name of the tool; prefixes most output.
|
|
23
17
|
*/
|
|
24
|
-
|
|
18
|
+
export const BIN = "saladplate";
|
|
25
19
|
const cwdForFilename = (filename) => {
|
|
26
|
-
return filename === "/dev/stdin" ?
|
|
20
|
+
return filename === "/dev/stdin" ? process.cwd() : path.dirname(filename);
|
|
27
21
|
};
|
|
28
22
|
const VAR_RE = /\$\{\{([^\}]+)\}\}/g;
|
|
29
23
|
const FILE_RE = /\$\<\<([^\>]+)\>\>/g;
|
|
@@ -37,24 +31,24 @@ function replaceAsync(input, regexp, fn) {
|
|
|
37
31
|
}
|
|
38
32
|
const replaceVar = (variable, filename, options) => {
|
|
39
33
|
var _a;
|
|
40
|
-
options.debug && console.debug(`${
|
|
41
|
-
return (_a =
|
|
34
|
+
options.debug && console.debug(`${BIN}: ${filename}: evaluating ${variable} for replacement...`);
|
|
35
|
+
return (_a = process.env[variable]) !== null && _a !== void 0 ? _a : "";
|
|
42
36
|
};
|
|
43
37
|
const replaceFile = (includeFilename, filename, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
44
38
|
// Resolve the include filename relative to the current file; if the current file is stdin
|
|
45
39
|
// then keep the current working directory.
|
|
46
40
|
const cwd = cwdForFilename(filename);
|
|
47
|
-
const include =
|
|
48
|
-
options.debug && console.debug(`${
|
|
49
|
-
const content = yield
|
|
50
|
-
return
|
|
41
|
+
const include = path.join(cwd, includeFilename);
|
|
42
|
+
options.debug && console.debug(`${BIN}: ${filename}: reading ${include} for replacement...`);
|
|
43
|
+
const content = yield fs.promises.readFile(include, { encoding: "utf-8" });
|
|
44
|
+
return template(content, include, options);
|
|
51
45
|
});
|
|
52
46
|
const replaceExec = (command, filename, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
47
|
// Execute the command in the working directory containing the file we are processing; if
|
|
54
48
|
// the current file is stdin then keep the current working directory.
|
|
55
49
|
const cwd = cwdForFilename(filename);
|
|
56
|
-
options.debug && console.debug(`${
|
|
57
|
-
const { stdout } = yield
|
|
50
|
+
options.debug && console.debug(`${BIN}: ${filename}: executing ${command} for replacement...`);
|
|
51
|
+
const { stdout } = yield util.promisify(exec)(command, {
|
|
58
52
|
cwd,
|
|
59
53
|
});
|
|
60
54
|
return stdout;
|
|
@@ -64,7 +58,7 @@ const replaceExec = (command, filename, options) => __awaiter(void 0, void 0, vo
|
|
|
64
58
|
* is assumed to have been read from the given `filename` (or stdin). Collapses newlines
|
|
65
59
|
* at the end of the output so that there's just one.
|
|
66
60
|
*/
|
|
67
|
-
const template = (content, filename, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
export const template = (content, filename, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
62
|
filename !== null && filename !== void 0 ? filename : (filename = "/dev/stdin");
|
|
69
63
|
options !== null && options !== void 0 ? options : (options = {});
|
|
70
64
|
content = yield replaceAsync(content, VAR_RE, (match) => {
|
|
@@ -79,4 +73,3 @@ const template = (content, filename, options) => __awaiter(void 0, void 0, void
|
|
|
79
73
|
content = content.replace(/\n+$/m, "\n");
|
|
80
74
|
return content;
|
|
81
75
|
});
|
|
82
|
-
exports.template = template;
|