@knocklabs/cli 0.1.0-rc.2 → 0.1.0-rc.4
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 +82 -6
- package/dist/commands/commit/index.js +4 -18
- package/dist/commands/commit/promote.js +4 -17
- package/dist/commands/translation/list.js +82 -0
- package/dist/commands/translation/pull.js +124 -0
- package/dist/commands/translation/push.js +130 -0
- package/dist/commands/translation/validate.js +122 -0
- package/dist/commands/workflow/activate.js +5 -18
- package/dist/commands/workflow/new.js +3 -3
- package/dist/commands/workflow/pull.js +70 -17
- package/dist/commands/workflow/push.js +3 -3
- package/dist/commands/workflow/validate.js +3 -3
- package/dist/lib/api-v1.js +38 -2
- package/dist/lib/base-command.js +2 -2
- package/dist/lib/helpers/error.js +16 -8
- package/dist/lib/helpers/flag.js +63 -3
- package/dist/lib/helpers/fs.js +52 -0
- package/dist/lib/helpers/json.js +6 -2
- package/dist/lib/helpers/object.js +43 -0
- package/dist/lib/helpers/page.js +3 -1
- package/dist/lib/helpers/request.js +17 -49
- package/dist/lib/helpers/ux.js +42 -0
- package/dist/lib/marshal/translation/helpers.js +185 -0
- package/dist/lib/marshal/translation/index.js +19 -0
- package/dist/lib/marshal/translation/reader.js +118 -0
- package/dist/lib/marshal/translation/types.js +4 -0
- package/dist/lib/marshal/translation/writer.js +86 -0
- package/dist/lib/marshal/workflow/generator.js +46 -5
- package/dist/lib/marshal/workflow/helpers.js +2 -0
- package/dist/lib/marshal/workflow/reader.js +136 -117
- package/dist/lib/marshal/workflow/writer.js +235 -98
- package/dist/lib/{helpers/dir-context.js → run-context/helpers.js} +1 -1
- package/dist/lib/run-context/index.js +22 -0
- package/dist/lib/{run-context.js → run-context/loader.js} +22 -7
- package/dist/lib/run-context/types.js +4 -0
- package/oclif.manifest.json +253 -1
- package/package.json +11 -10
- package/dist/lib/helpers/spinner.js +0 -20
|
@@ -2,76 +2,44 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
isSuccessResp: ()=>isSuccessResp,
|
|
13
|
+
formatErrorRespMessage: ()=>formatErrorRespMessage,
|
|
14
|
+
withSpinner: ()=>withSpinner
|
|
8
15
|
});
|
|
9
16
|
const _core = require("@oclif/core");
|
|
10
17
|
const _error = require("./error");
|
|
11
|
-
const
|
|
12
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
13
|
-
if (typeof WeakMap !== "function") return null;
|
|
14
|
-
var cacheBabelInterop = new WeakMap();
|
|
15
|
-
var cacheNodeInterop = new WeakMap();
|
|
16
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
17
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
18
|
-
})(nodeInterop);
|
|
19
|
-
}
|
|
20
|
-
function _interopRequireWildcard(obj, nodeInterop) {
|
|
21
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
22
|
-
return obj;
|
|
23
|
-
}
|
|
24
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
25
|
-
return {
|
|
26
|
-
default: obj
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
30
|
-
if (cache && cache.has(obj)) {
|
|
31
|
-
return cache.get(obj);
|
|
32
|
-
}
|
|
33
|
-
var newObj = {};
|
|
34
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
35
|
-
for(var key in obj){
|
|
36
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
37
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
38
|
-
if (desc && (desc.get || desc.set)) {
|
|
39
|
-
Object.defineProperty(newObj, key, desc);
|
|
40
|
-
} else {
|
|
41
|
-
newObj[key] = obj[key];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
newObj.default = obj;
|
|
46
|
-
if (cache) {
|
|
47
|
-
cache.set(obj, newObj);
|
|
48
|
-
}
|
|
49
|
-
return newObj;
|
|
50
|
-
}
|
|
18
|
+
const _ux = require("./ux");
|
|
51
19
|
const isSuccessResp = (resp)=>resp.status >= 200 && resp.status < 300;
|
|
52
|
-
|
|
53
|
-
* Returns a formatted error message from an error response based on status code.
|
|
54
|
-
*/ const formatErrorRespMessage = ({ status , data })=>{
|
|
20
|
+
const formatErrorRespMessage = ({ status , data })=>{
|
|
55
21
|
if (status === 500) {
|
|
56
22
|
return "An internal server error occurred";
|
|
57
23
|
}
|
|
58
24
|
const { message , errors =[] } = data;
|
|
59
25
|
if (status >= 400) {
|
|
60
26
|
const errs = errors.map((e)=>new _error.JsonDataError(e.message, e.field));
|
|
61
|
-
return errs.length === 0 ? message : message + "\n\n" + (0, _error.formatErrors)(errs
|
|
27
|
+
return errs.length === 0 ? message : message + "\n\n" + (0, _error.formatErrors)(errs, {
|
|
28
|
+
indentBy: 2
|
|
29
|
+
});
|
|
62
30
|
}
|
|
63
31
|
return message;
|
|
64
32
|
};
|
|
65
33
|
const withSpinner = async (requestFn, opts = {})=>{
|
|
66
34
|
const { action ="‣ Loading" , ensureSuccess =true } = opts;
|
|
67
35
|
// Suppress printing the spinner in tests, oclif doesn't for some reasons.
|
|
68
|
-
|
|
36
|
+
_ux.spinner.start(action);
|
|
69
37
|
const resp = await requestFn();
|
|
70
38
|
// Error out before the action stop so the spinner can update accordingly.
|
|
71
39
|
if (ensureSuccess && !isSuccessResp(resp)) {
|
|
72
40
|
const message = formatErrorRespMessage(resp);
|
|
73
41
|
_core.CliUx.ux.error(new _error.ApiError(message));
|
|
74
42
|
}
|
|
75
|
-
|
|
43
|
+
_ux.spinner.stop();
|
|
76
44
|
return resp;
|
|
77
45
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
promptToConfirm: ()=>promptToConfirm,
|
|
13
|
+
spinner: ()=>spinner
|
|
14
|
+
});
|
|
15
|
+
const _core = require("@oclif/core");
|
|
16
|
+
const _enquirer = /*#__PURE__*/ _interopRequireDefault(require("enquirer"));
|
|
17
|
+
const _const = require("./const");
|
|
18
|
+
function _interopRequireDefault(obj) {
|
|
19
|
+
return obj && obj.__esModule ? obj : {
|
|
20
|
+
default: obj
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const promptToConfirm = async (message)=>{
|
|
24
|
+
try {
|
|
25
|
+
const { input } = await _enquirer.default.prompt({
|
|
26
|
+
type: "confirm",
|
|
27
|
+
name: "input",
|
|
28
|
+
message
|
|
29
|
+
});
|
|
30
|
+
return input;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.log(error);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const spinner = {
|
|
36
|
+
start (action) {
|
|
37
|
+
if (!_const.isTestEnv) _core.CliUx.ux.action.start(action);
|
|
38
|
+
},
|
|
39
|
+
stop () {
|
|
40
|
+
_core.CliUx.ux.action.stop();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
isValidLocale: ()=>isValidLocale,
|
|
13
|
+
isTranslationDir: ()=>isTranslationDir,
|
|
14
|
+
buildTranslationFileCtx: ()=>buildTranslationFileCtx,
|
|
15
|
+
parseTranslationRef: ()=>parseTranslationRef,
|
|
16
|
+
ensureValidCommandTarget: ()=>ensureValidCommandTarget,
|
|
17
|
+
lsTranslationDir: ()=>lsTranslationDir
|
|
18
|
+
});
|
|
19
|
+
const _nodePath = /*#__PURE__*/ _interopRequireWildcard(require("node:path"));
|
|
20
|
+
const _core = require("@oclif/core");
|
|
21
|
+
const _fsExtra = /*#__PURE__*/ _interopRequireWildcard(require("fs-extra"));
|
|
22
|
+
const _localeCodes = /*#__PURE__*/ _interopRequireDefault(require("locale-codes"));
|
|
23
|
+
const _fs = require("../../helpers/fs");
|
|
24
|
+
function _interopRequireDefault(obj) {
|
|
25
|
+
return obj && obj.__esModule ? obj : {
|
|
26
|
+
default: obj
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
30
|
+
if (typeof WeakMap !== "function") return null;
|
|
31
|
+
var cacheBabelInterop = new WeakMap();
|
|
32
|
+
var cacheNodeInterop = new WeakMap();
|
|
33
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
34
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
35
|
+
})(nodeInterop);
|
|
36
|
+
}
|
|
37
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
38
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
39
|
+
return obj;
|
|
40
|
+
}
|
|
41
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
42
|
+
return {
|
|
43
|
+
default: obj
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
47
|
+
if (cache && cache.has(obj)) {
|
|
48
|
+
return cache.get(obj);
|
|
49
|
+
}
|
|
50
|
+
var newObj = {};
|
|
51
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
52
|
+
for(var key in obj){
|
|
53
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
54
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
55
|
+
if (desc && (desc.get || desc.set)) {
|
|
56
|
+
Object.defineProperty(newObj, key, desc);
|
|
57
|
+
} else {
|
|
58
|
+
newObj[key] = obj[key];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
newObj.default = obj;
|
|
63
|
+
if (cache) {
|
|
64
|
+
cache.set(obj, newObj);
|
|
65
|
+
}
|
|
66
|
+
return newObj;
|
|
67
|
+
}
|
|
68
|
+
const isValidLocale = (localeCode)=>Boolean(_localeCodes.default.getByTag(localeCode));
|
|
69
|
+
const isTranslationDir = (dirPath)=>{
|
|
70
|
+
const locale = _nodePath.basename(dirPath);
|
|
71
|
+
return isValidLocale(locale);
|
|
72
|
+
};
|
|
73
|
+
const buildTranslationFileCtx = async (dirPath, localeCode, namespace)=>{
|
|
74
|
+
const ref = namespace ? `${namespace}.${localeCode}` : localeCode;
|
|
75
|
+
const abspath = _nodePath.resolve(dirPath, `${ref}.json`);
|
|
76
|
+
const exists = await _fsExtra.pathExists(abspath);
|
|
77
|
+
return {
|
|
78
|
+
ref,
|
|
79
|
+
localeCode,
|
|
80
|
+
namespace,
|
|
81
|
+
abspath,
|
|
82
|
+
exists
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
const parseTranslationRef = (reference)=>{
|
|
86
|
+
const strings = reference.split(".");
|
|
87
|
+
// Locale code only (e.g. `en`)
|
|
88
|
+
if (strings.length === 1) {
|
|
89
|
+
return {
|
|
90
|
+
localeCode: strings[0]
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// Locale code prefixed with a namespace (e.g. `admin.en`)
|
|
94
|
+
if (strings.length === 2) {
|
|
95
|
+
return {
|
|
96
|
+
localeCode: strings[1],
|
|
97
|
+
namespace: strings[0]
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
// Invalid pattern.
|
|
101
|
+
return undefined;
|
|
102
|
+
};
|
|
103
|
+
const ensureValidCommandTarget = async (props, runContext)=>{
|
|
104
|
+
const { flags , args } = props;
|
|
105
|
+
const { commandId , resourceDir: resourceDirCtx , cwd: runCwd } = runContext;
|
|
106
|
+
// Error, trying to run the command not in a translation directory.
|
|
107
|
+
if (resourceDirCtx && resourceDirCtx.type !== "translation") {
|
|
108
|
+
return _core.CliUx.ux.error(`Cannot run ${commandId} inside a ${resourceDirCtx.type} directory`);
|
|
109
|
+
}
|
|
110
|
+
// Error, got neither the translationRef arg nor the --all flag.
|
|
111
|
+
if (!args.translationRef && !flags.all) {
|
|
112
|
+
_core.CliUx.ux.error("At least one of translation ref arg or --all flag must be given");
|
|
113
|
+
}
|
|
114
|
+
// No translationRef arg, which means --all flag is used.
|
|
115
|
+
if (!args.translationRef) {
|
|
116
|
+
// Targeting all translation files in the current locale directory.
|
|
117
|
+
if (resourceDirCtx) {
|
|
118
|
+
return {
|
|
119
|
+
type: "translationDir",
|
|
120
|
+
context: resourceDirCtx
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// Targeting all translation files in the translations index dir.
|
|
124
|
+
// TODO: Default to the knock project config first if present before cwd.
|
|
125
|
+
const defaultToCwd = {
|
|
126
|
+
abspath: runCwd,
|
|
127
|
+
exists: true
|
|
128
|
+
};
|
|
129
|
+
const indexDirCtx = flags["translations-dir"] || defaultToCwd;
|
|
130
|
+
return {
|
|
131
|
+
type: "translationsIndexDir",
|
|
132
|
+
context: indexDirCtx
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// From this point on, we have translationRef so parse and validate the format.
|
|
136
|
+
const parsedRef = parseTranslationRef(args.translationRef);
|
|
137
|
+
if (!parsedRef) {
|
|
138
|
+
return _core.CliUx.ux.error(`Invalid translationRef arg \`${args.translationRef}\`, use valid <locale> or <namespace>.<locale> for namespaced translations`);
|
|
139
|
+
}
|
|
140
|
+
const { localeCode , namespace } = parsedRef;
|
|
141
|
+
// If we are in the translation dir, make sure the locale matches.
|
|
142
|
+
if (resourceDirCtx && resourceDirCtx.key !== localeCode) {
|
|
143
|
+
return _core.CliUx.ux.error(`Cannot run ${commandId} with \`${args.translationRef}\` inside a ${resourceDirCtx.key} directory`);
|
|
144
|
+
}
|
|
145
|
+
const targetDirPath = resourceDirCtx ? resourceDirCtx.abspath : _nodePath.resolve(runCwd, localeCode);
|
|
146
|
+
// Got translationRef arg but no --all flag, which means target only a single
|
|
147
|
+
// translation file.
|
|
148
|
+
if (!flags.all) {
|
|
149
|
+
const translationFileCtx = await buildTranslationFileCtx(targetDirPath, localeCode, namespace);
|
|
150
|
+
return {
|
|
151
|
+
type: "translationFile",
|
|
152
|
+
context: translationFileCtx
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
// From this point on, we have both translationRef and --all flag used
|
|
156
|
+
// together, so make sure we are targeting a non-namespaced top-level locale.
|
|
157
|
+
if (namespace) {
|
|
158
|
+
return _core.CliUx.ux.error(`Cannot use --all with a namespaced translation \`${args.translationRef}\``);
|
|
159
|
+
}
|
|
160
|
+
const translationDirCtx = {
|
|
161
|
+
type: "translation",
|
|
162
|
+
key: localeCode,
|
|
163
|
+
abspath: targetDirPath,
|
|
164
|
+
exists: await (0, _fs.isDirectory)(targetDirPath)
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
type: "translationDir",
|
|
168
|
+
context: translationDirCtx
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
const lsTranslationDir = async (pathToDir)=>{
|
|
172
|
+
const localeCode = _nodePath.basename(pathToDir).toLowerCase();
|
|
173
|
+
const dirents = await _fsExtra.readdir(pathToDir, {
|
|
174
|
+
withFileTypes: true
|
|
175
|
+
});
|
|
176
|
+
return dirents.filter((dirent)=>{
|
|
177
|
+
if (dirent.isDirectory()) return false;
|
|
178
|
+
const filename = dirent.name.toLowerCase();
|
|
179
|
+
if (!filename.endsWith(`${localeCode}.json`)) return false;
|
|
180
|
+
const { name: translationRef } = _nodePath.parse(filename);
|
|
181
|
+
const parsedRef = parseTranslationRef(translationRef);
|
|
182
|
+
if (!parsedRef) return false;
|
|
183
|
+
return true;
|
|
184
|
+
}).map((dirent)=>_nodePath.resolve(pathToDir, dirent.name));
|
|
185
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
_exportStar(require("./helpers"), exports);
|
|
6
|
+
_exportStar(require("./reader"), exports);
|
|
7
|
+
_exportStar(require("./types"), exports);
|
|
8
|
+
_exportStar(require("./writer"), exports);
|
|
9
|
+
function _exportStar(from, to) {
|
|
10
|
+
Object.keys(from).forEach(function(k) {
|
|
11
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function() {
|
|
14
|
+
return from[k];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
return from;
|
|
19
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "readTranslationFilesForCommandTarget", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>readTranslationFilesForCommandTarget
|
|
8
|
+
});
|
|
9
|
+
const _nodePath = /*#__PURE__*/ _interopRequireWildcard(require("node:path"));
|
|
10
|
+
const _core = require("@oclif/core");
|
|
11
|
+
const _fsExtra = /*#__PURE__*/ _interopRequireWildcard(require("fs-extra"));
|
|
12
|
+
const _error = require("../../helpers/error");
|
|
13
|
+
const _json = require("../../helpers/json");
|
|
14
|
+
const _helpers = require("./helpers");
|
|
15
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
16
|
+
if (typeof WeakMap !== "function") return null;
|
|
17
|
+
var cacheBabelInterop = new WeakMap();
|
|
18
|
+
var cacheNodeInterop = new WeakMap();
|
|
19
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
20
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
21
|
+
})(nodeInterop);
|
|
22
|
+
}
|
|
23
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
24
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
25
|
+
return obj;
|
|
26
|
+
}
|
|
27
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
28
|
+
return {
|
|
29
|
+
default: obj
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
33
|
+
if (cache && cache.has(obj)) {
|
|
34
|
+
return cache.get(obj);
|
|
35
|
+
}
|
|
36
|
+
var newObj = {};
|
|
37
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
+
for(var key in obj){
|
|
39
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
40
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
41
|
+
if (desc && (desc.get || desc.set)) {
|
|
42
|
+
Object.defineProperty(newObj, key, desc);
|
|
43
|
+
} else {
|
|
44
|
+
newObj[key] = obj[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
newObj.default = obj;
|
|
49
|
+
if (cache) {
|
|
50
|
+
cache.set(obj, newObj);
|
|
51
|
+
}
|
|
52
|
+
return newObj;
|
|
53
|
+
}
|
|
54
|
+
/*
|
|
55
|
+
* For the given list of translation file paths, read each file and return
|
|
56
|
+
* translation file data.
|
|
57
|
+
*
|
|
58
|
+
* Note, it assumes they are valid file paths to translation files.
|
|
59
|
+
*/ const readTranslationFiles = async (filePaths)=>{
|
|
60
|
+
const translations = [];
|
|
61
|
+
const errors = [];
|
|
62
|
+
for (const abspath of filePaths){
|
|
63
|
+
const { name: translationRef } = _nodePath.parse(abspath);
|
|
64
|
+
const parsedRef = (0, _helpers.parseTranslationRef)(translationRef);
|
|
65
|
+
if (!parsedRef) continue;
|
|
66
|
+
const { localeCode , namespace } = parsedRef;
|
|
67
|
+
// eslint-disable-next-line no-await-in-loop
|
|
68
|
+
const [content, readJsonErrors] = await (0, _json.readJson)(abspath);
|
|
69
|
+
if (readJsonErrors.length > 0) {
|
|
70
|
+
const e = new _error.SourceError((0, _error.formatErrors)(readJsonErrors), abspath);
|
|
71
|
+
errors.push(e);
|
|
72
|
+
}
|
|
73
|
+
if (content) {
|
|
74
|
+
translations.push({
|
|
75
|
+
ref: translationRef,
|
|
76
|
+
localeCode,
|
|
77
|
+
namespace,
|
|
78
|
+
abspath,
|
|
79
|
+
exists: true,
|
|
80
|
+
content: JSON.stringify(content)
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return [
|
|
85
|
+
translations,
|
|
86
|
+
errors
|
|
87
|
+
];
|
|
88
|
+
};
|
|
89
|
+
const readTranslationFilesForCommandTarget = async (target)=>{
|
|
90
|
+
const { type: targetType , context: targetCtx } = target;
|
|
91
|
+
if (!targetCtx.exists) {
|
|
92
|
+
return _core.CliUx.ux.error(`Cannot locate translation file(s) at \`${targetCtx.abspath}\``);
|
|
93
|
+
}
|
|
94
|
+
switch(targetType){
|
|
95
|
+
case "translationFile":
|
|
96
|
+
{
|
|
97
|
+
return readTranslationFiles([
|
|
98
|
+
targetCtx.abspath
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
case "translationDir":
|
|
102
|
+
{
|
|
103
|
+
const translationFilePaths = await (0, _helpers.lsTranslationDir)(targetCtx.abspath);
|
|
104
|
+
return readTranslationFiles(translationFilePaths);
|
|
105
|
+
}
|
|
106
|
+
case "translationsIndexDir":
|
|
107
|
+
{
|
|
108
|
+
const dirents = await _fsExtra.readdir(targetCtx.abspath, {
|
|
109
|
+
withFileTypes: true
|
|
110
|
+
});
|
|
111
|
+
const translationDirPaths = dirents.filter((dirent)=>dirent.isDirectory() && (0, _helpers.isTranslationDir)(dirent.name)).map((dirent)=>_nodePath.resolve(targetCtx.abspath, dirent.name));
|
|
112
|
+
const translationFilePaths = (await Promise.all(translationDirPaths.map(async (abspath)=>(0, _helpers.lsTranslationDir)(abspath)))).flat();
|
|
113
|
+
return readTranslationFiles(translationFilePaths);
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
throw new Error(`Invalid translation command target: ${target}`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "writeTranslationsIndexDir", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>writeTranslationsIndexDir
|
|
8
|
+
});
|
|
9
|
+
const _nodePath = /*#__PURE__*/ _interopRequireWildcard(require("node:path"));
|
|
10
|
+
const _fsExtra = /*#__PURE__*/ _interopRequireWildcard(require("fs-extra"));
|
|
11
|
+
const _lodash = require("lodash");
|
|
12
|
+
const _const = require("../../helpers/const");
|
|
13
|
+
const _json = require("../../helpers/json");
|
|
14
|
+
const _helpers = require("./helpers");
|
|
15
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
16
|
+
if (typeof WeakMap !== "function") return null;
|
|
17
|
+
var cacheBabelInterop = new WeakMap();
|
|
18
|
+
var cacheNodeInterop = new WeakMap();
|
|
19
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
20
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
21
|
+
})(nodeInterop);
|
|
22
|
+
}
|
|
23
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
24
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
25
|
+
return obj;
|
|
26
|
+
}
|
|
27
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
28
|
+
return {
|
|
29
|
+
default: obj
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
33
|
+
if (cache && cache.has(obj)) {
|
|
34
|
+
return cache.get(obj);
|
|
35
|
+
}
|
|
36
|
+
var newObj = {};
|
|
37
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
38
|
+
for(var key in obj){
|
|
39
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
40
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
41
|
+
if (desc && (desc.get || desc.set)) {
|
|
42
|
+
Object.defineProperty(newObj, key, desc);
|
|
43
|
+
} else {
|
|
44
|
+
newObj[key] = obj[key];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
newObj.default = obj;
|
|
49
|
+
if (cache) {
|
|
50
|
+
cache.set(obj, newObj);
|
|
51
|
+
}
|
|
52
|
+
return newObj;
|
|
53
|
+
}
|
|
54
|
+
const writeTranslationsIndexDir = async (indexDirCtx, translations)=>{
|
|
55
|
+
const backupDirPath = _nodePath.resolve(_const.sandboxDir, (0, _lodash.uniqueId)("backup"));
|
|
56
|
+
try {
|
|
57
|
+
// If the index directory already exists, back it up in the temp sandbox
|
|
58
|
+
// before wiping it clean.
|
|
59
|
+
if (indexDirCtx.exists) {
|
|
60
|
+
await _fsExtra.copy(indexDirCtx.abspath, backupDirPath);
|
|
61
|
+
await _fsExtra.emptyDir(indexDirCtx.abspath);
|
|
62
|
+
}
|
|
63
|
+
// Write given remote translations into the given translations directory path.
|
|
64
|
+
const writeTranslationDirPromises = translations.map(async (translation)=>{
|
|
65
|
+
const translationDirPath = _nodePath.resolve(indexDirCtx.abspath, translation.locale_code);
|
|
66
|
+
const translationFileCtx = await (0, _helpers.buildTranslationFileCtx)(translationDirPath, translation.locale_code, translation.namespace);
|
|
67
|
+
return _fsExtra.outputJson(translationFileCtx.abspath, JSON.parse(translation.content), {
|
|
68
|
+
spaces: _json.DOUBLE_SPACES
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
await Promise.all(writeTranslationDirPromises);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// In case of any error, wipe the index directory that is likely in a bad
|
|
74
|
+
// state then restore the backup if one existed before.
|
|
75
|
+
if (indexDirCtx.exists) {
|
|
76
|
+
await _fsExtra.emptyDir(indexDirCtx.abspath);
|
|
77
|
+
await _fsExtra.copy(backupDirPath, indexDirCtx.abspath);
|
|
78
|
+
} else {
|
|
79
|
+
await _fsExtra.remove(indexDirCtx.abspath);
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
} finally{
|
|
83
|
+
// Always clean up the backup directory in the temp sandbox.
|
|
84
|
+
await _fsExtra.remove(backupDirPath);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
@@ -13,10 +13,51 @@ _export(exports, {
|
|
|
13
13
|
generateWorkflowDir: ()=>generateWorkflowDir,
|
|
14
14
|
scaffoldWorkflowDirBundle: ()=>scaffoldWorkflowDirBundle
|
|
15
15
|
});
|
|
16
|
+
const _nodePath = /*#__PURE__*/ _interopRequireWildcard(require("node:path"));
|
|
16
17
|
const _lodash = require("lodash");
|
|
17
18
|
const _helpers = require("./helpers");
|
|
18
19
|
const _types = require("./types");
|
|
19
20
|
const _writer = require("./writer");
|
|
21
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
22
|
+
if (typeof WeakMap !== "function") return null;
|
|
23
|
+
var cacheBabelInterop = new WeakMap();
|
|
24
|
+
var cacheNodeInterop = new WeakMap();
|
|
25
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
26
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
27
|
+
})(nodeInterop);
|
|
28
|
+
}
|
|
29
|
+
function _interopRequireWildcard(obj, nodeInterop) {
|
|
30
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
31
|
+
return obj;
|
|
32
|
+
}
|
|
33
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
34
|
+
return {
|
|
35
|
+
default: obj
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
39
|
+
if (cache && cache.has(obj)) {
|
|
40
|
+
return cache.get(obj);
|
|
41
|
+
}
|
|
42
|
+
var newObj = {};
|
|
43
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
44
|
+
for(var key in obj){
|
|
45
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
46
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
47
|
+
if (desc && (desc.get || desc.set)) {
|
|
48
|
+
Object.defineProperty(newObj, key, desc);
|
|
49
|
+
} else {
|
|
50
|
+
newObj[key] = obj[key];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
newObj.default = obj;
|
|
55
|
+
if (cache) {
|
|
56
|
+
cache.set(obj, newObj);
|
|
57
|
+
}
|
|
58
|
+
return newObj;
|
|
59
|
+
}
|
|
60
|
+
const newTemplateFilePath = (stepRef, fileName, fileExt)=>_nodePath.join(stepRef, `${fileName}.${fileExt}`).toLowerCase();
|
|
20
61
|
const scaffoldDelayStep = (refSuffix)=>{
|
|
21
62
|
const scaffoldedStep = {
|
|
22
63
|
ref: `${_types.StepType.Delay}_${refSuffix}`,
|
|
@@ -66,7 +107,7 @@ const scaffoldHttpFetchStep = (refSuffix)=>{
|
|
|
66
107
|
};
|
|
67
108
|
const scaffoldEmailChannelStep = (refSuffix)=>{
|
|
68
109
|
const stepRef = `email_${refSuffix}`;
|
|
69
|
-
const templateFilePath =
|
|
110
|
+
const templateFilePath = newTemplateFilePath(stepRef, "html_body", "html");
|
|
70
111
|
const scaffoldedStep = {
|
|
71
112
|
ref: stepRef,
|
|
72
113
|
type: _types.StepType.Channel,
|
|
@@ -89,7 +130,7 @@ const scaffoldEmailChannelStep = (refSuffix)=>{
|
|
|
89
130
|
};
|
|
90
131
|
const scaffoldInAppFeedChannelStep = (refSuffix)=>{
|
|
91
132
|
const stepRef = `in_app_feed_${refSuffix}`;
|
|
92
|
-
const templateFilePath =
|
|
133
|
+
const templateFilePath = newTemplateFilePath(stepRef, "markdown_body", "md");
|
|
93
134
|
const scaffoldedStep = {
|
|
94
135
|
ref: stepRef,
|
|
95
136
|
type: _types.StepType.Channel,
|
|
@@ -109,7 +150,7 @@ const scaffoldInAppFeedChannelStep = (refSuffix)=>{
|
|
|
109
150
|
};
|
|
110
151
|
const scaffoldSmsChannelStep = (refSuffix)=>{
|
|
111
152
|
const stepRef = `sms_${refSuffix}`;
|
|
112
|
-
const templateFilePath =
|
|
153
|
+
const templateFilePath = newTemplateFilePath(stepRef, "text_body", "txt");
|
|
113
154
|
const scaffoldedStep = {
|
|
114
155
|
ref: stepRef,
|
|
115
156
|
type: _types.StepType.Channel,
|
|
@@ -128,7 +169,7 @@ const scaffoldSmsChannelStep = (refSuffix)=>{
|
|
|
128
169
|
};
|
|
129
170
|
const scaffoldPushChannelStep = (refSuffix)=>{
|
|
130
171
|
const stepRef = `push_${refSuffix}`;
|
|
131
|
-
const templateFilePath =
|
|
172
|
+
const templateFilePath = newTemplateFilePath(stepRef, "text_body", "txt");
|
|
132
173
|
const scaffoldedStep = {
|
|
133
174
|
ref: stepRef,
|
|
134
175
|
type: _types.StepType.Channel,
|
|
@@ -150,7 +191,7 @@ const scaffoldPushChannelStep = (refSuffix)=>{
|
|
|
150
191
|
};
|
|
151
192
|
const scaffoldChatChannelStep = (refSuffix)=>{
|
|
152
193
|
const stepRef = `chat_${refSuffix}`;
|
|
153
|
-
const templateFilePath =
|
|
194
|
+
const templateFilePath = newTemplateFilePath(stepRef, "markdown_body", "md");
|
|
154
195
|
const scaffoldedStep = {
|
|
155
196
|
ref: stepRef,
|
|
156
197
|
type: _types.StepType.Channel,
|
|
@@ -10,6 +10,7 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
WORKFLOW_JSON: ()=>WORKFLOW_JSON,
|
|
13
|
+
VISUAL_BLOCKS_JSON: ()=>VISUAL_BLOCKS_JSON,
|
|
13
14
|
FILEPATH_MARKER: ()=>FILEPATH_MARKER,
|
|
14
15
|
FILEPATH_MARKED_RE: ()=>FILEPATH_MARKED_RE,
|
|
15
16
|
validateWorkflowKey: ()=>validateWorkflowKey,
|
|
@@ -64,6 +65,7 @@ function _interopRequireWildcard(obj, nodeInterop) {
|
|
|
64
65
|
return newObj;
|
|
65
66
|
}
|
|
66
67
|
const WORKFLOW_JSON = "workflow.json";
|
|
68
|
+
const VISUAL_BLOCKS_JSON = "visual_blocks.json";
|
|
67
69
|
const FILEPATH_MARKER = "@";
|
|
68
70
|
const FILEPATH_MARKED_RE = new RegExp(`${FILEPATH_MARKER}$`);
|
|
69
71
|
const validateWorkflowKey = (input)=>{
|