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