@featurevisor/core 4.3.0 → 4.4.0
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/lib/cli/cli.d.ts +2 -1
- package/lib/cli/cli.js +9 -6
- package/lib/cli/cli.js.map +1 -1
- package/lib/cli/options.js +61 -0
- package/lib/cli/options.js.map +1 -1
- package/lib/cli/plugins.js +7 -1
- package/lib/cli/plugins.js.map +1 -1
- package/lib/config/projectConfig.js +6 -0
- package/lib/config/projectConfig.js.map +1 -1
- package/lib/convert/check.d.ts +33 -0
- package/lib/convert/check.js +377 -0
- package/lib/convert/check.js.map +1 -0
- package/lib/convert/config.d.ts +20 -0
- package/lib/convert/config.js +147 -0
- package/lib/convert/config.js.map +1 -0
- package/lib/convert/documents.d.ts +55 -0
- package/lib/convert/documents.js +195 -0
- package/lib/convert/documents.js.map +1 -0
- package/lib/convert/index.d.ts +7 -0
- package/lib/convert/index.js +35 -0
- package/lib/convert/index.js.map +1 -0
- package/lib/convert/plan.d.ts +41 -0
- package/lib/convert/plan.js +562 -0
- package/lib/convert/plan.js.map +1 -0
- package/lib/convert/project.d.ts +29 -0
- package/lib/convert/project.js +109 -0
- package/lib/convert/project.js.map +1 -0
- package/lib/convert/run.d.ts +3 -0
- package/lib/convert/run.js +224 -0
- package/lib/convert/run.js.map +1 -0
- package/lib/convert/tests.d.ts +22 -0
- package/lib/convert/tests.js +267 -0
- package/lib/convert/tests.js.map +1 -0
- package/lib/datasource/datasource.d.ts +2 -0
- package/lib/datasource/datasource.js +8 -0
- package/lib/datasource/datasource.js.map +1 -1
- package/lib/datasource/parseError.d.ts +11 -0
- package/lib/datasource/parseError.js +33 -19
- package/lib/datasource/parseError.js.map +1 -1
- package/lib/formatter/formatDefinition.d.ts +32 -0
- package/lib/formatter/formatDefinition.js +161 -0
- package/lib/formatter/formatDefinition.js.map +1 -0
- package/lib/formatter/formatProject.d.ts +68 -0
- package/lib/formatter/formatProject.js +184 -0
- package/lib/formatter/formatProject.js.map +1 -0
- package/lib/formatter/index.d.ts +6 -0
- package/lib/formatter/index.js +49 -0
- package/lib/formatter/index.js.map +1 -0
- package/lib/formatter/layout.d.ts +26 -0
- package/lib/formatter/layout.js +175 -0
- package/lib/formatter/layout.js.map +1 -0
- package/lib/formatter/printFormatResult.d.ts +8 -0
- package/lib/formatter/printFormatResult.js +98 -0
- package/lib/formatter/printFormatResult.js.map +1 -0
- package/lib/formatter/run.d.ts +6 -0
- package/lib/formatter/run.js +92 -0
- package/lib/formatter/run.js.map +1 -0
- package/lib/formatter/since.d.ts +6 -0
- package/lib/formatter/since.js +80 -0
- package/lib/formatter/since.js.map +1 -0
- package/lib/formatter/style.d.ts +94 -0
- package/lib/formatter/style.js +237 -0
- package/lib/formatter/style.js.map +1 -0
- package/lib/linter/diagnostics.d.ts +12 -12
- package/lib/linter/featureSchema.js +14 -13
- package/lib/linter/featureSchema.js.map +1 -1
- package/lib/migrate/definitions.d.ts +46 -0
- package/lib/migrate/definitions.js +488 -0
- package/lib/migrate/definitions.js.map +1 -0
- package/lib/migrate/index.d.ts +6 -0
- package/lib/migrate/index.js +30 -0
- package/lib/migrate/index.js.map +1 -0
- package/lib/migrate/migrateProject.d.ts +33 -0
- package/lib/migrate/migrateProject.js +195 -0
- package/lib/migrate/migrateProject.js.map +1 -0
- package/lib/migrate/project.d.ts +31 -0
- package/lib/migrate/project.js +153 -0
- package/lib/migrate/project.js.map +1 -0
- package/lib/migrate/run.d.ts +3 -0
- package/lib/migrate/run.js +81 -0
- package/lib/migrate/run.js.map +1 -0
- package/lib/migrate/write.d.ts +10 -0
- package/lib/migrate/write.js +150 -0
- package/lib/migrate/write.js.map +1 -0
- package/lib/promoter/index.js +38 -2
- package/lib/promoter/index.js.map +1 -1
- package/lib/rewrite/diff.d.ts +2 -0
- package/lib/rewrite/diff.js +76 -0
- package/lib/rewrite/diff.js.map +1 -0
- package/lib/rewrite/files.d.ts +6 -0
- package/lib/rewrite/files.js +66 -0
- package/lib/rewrite/files.js.map +1 -0
- package/lib/rewrite/lines.d.ts +56 -0
- package/lib/rewrite/lines.js +204 -0
- package/lib/rewrite/lines.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.migrateProject = migrateProject;
|
|
37
|
+
const fs = __importStar(require("node:fs"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const projectConfig_1 = require("../config/projectConfig");
|
|
40
|
+
const parseError_1 = require("../datasource/parseError");
|
|
41
|
+
const diff_1 = require("../rewrite/diff");
|
|
42
|
+
const lines_1 = require("../rewrite/lines");
|
|
43
|
+
const definitions_1 = require("./definitions");
|
|
44
|
+
const project_1 = require("./project");
|
|
45
|
+
const write_1 = require("./write");
|
|
46
|
+
const GUIDE = "https://featurevisor.com/docs/migrations/v4/";
|
|
47
|
+
const link = (section) => `${GUIDE}#${section}`;
|
|
48
|
+
const relative = (filePath) => path.relative(process.cwd(), filePath) || filePath;
|
|
49
|
+
/** An attribute's key, from its file name */
|
|
50
|
+
function attributeKey(file, extension) {
|
|
51
|
+
return path.basename(file.filePath, `.${extension}`);
|
|
52
|
+
}
|
|
53
|
+
function migrateProject(rootDirectoryPath, options = {}) {
|
|
54
|
+
const project = (0, project_1.readProject)(rootDirectoryPath);
|
|
55
|
+
const result = {
|
|
56
|
+
files: [],
|
|
57
|
+
blockers: project.blockers.map(({ message, section }) => ({ message, link: link(section) })),
|
|
58
|
+
total: project.files.length,
|
|
59
|
+
migrated: 0,
|
|
60
|
+
errors: 0,
|
|
61
|
+
};
|
|
62
|
+
if (result.blockers.length > 0)
|
|
63
|
+
return result;
|
|
64
|
+
const write = !options.check && !options.diff;
|
|
65
|
+
const { parser } = project;
|
|
66
|
+
const read = new Map();
|
|
67
|
+
const report = (file, entry) => {
|
|
68
|
+
if (entry.changes.length === 0 && entry.notes.length === 0 && !entry.error)
|
|
69
|
+
return;
|
|
70
|
+
result.files.push({
|
|
71
|
+
filePath: relative(file.filePath),
|
|
72
|
+
...(file.set ? { set: file.set } : {}),
|
|
73
|
+
...entry,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
for (const file of project.files) {
|
|
77
|
+
const source = fs.readFileSync(file.filePath, "utf8");
|
|
78
|
+
try {
|
|
79
|
+
read.set(file, { source, value: parser.parse(source, file.filePath) });
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const { message, line, column } = (0, parseError_1.describeParseError)(error);
|
|
83
|
+
const where = line ? ` (line ${line}${column ? `, column ${column}` : ""})` : "";
|
|
84
|
+
report(file, { changes: [], notes: [], error: `${message}${where}` });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// attributes first, so conditions can be checked against their migrated types
|
|
88
|
+
const migrated = new Map();
|
|
89
|
+
const attributes = new Map();
|
|
90
|
+
for (const [file, { value }] of read) {
|
|
91
|
+
if (file.kind !== "attribute")
|
|
92
|
+
continue;
|
|
93
|
+
const migration = new definitions_1.Migration();
|
|
94
|
+
const after = (0, definitions_1.migrateAttribute)(value, migration);
|
|
95
|
+
migrated.set(file, { value: after, migration });
|
|
96
|
+
const set = attributes.get(file.set || "") || {};
|
|
97
|
+
set[attributeKey(file, parser.extension)] = after;
|
|
98
|
+
attributes.set(file.set || "", set);
|
|
99
|
+
}
|
|
100
|
+
for (const [file, { value }] of read) {
|
|
101
|
+
if (file.kind === "attribute")
|
|
102
|
+
continue;
|
|
103
|
+
const migration = new definitions_1.Migration();
|
|
104
|
+
const inSet = attributes.get(file.set || "") || {};
|
|
105
|
+
const kinds = {
|
|
106
|
+
attribute: () => value,
|
|
107
|
+
schema: () => (0, definitions_1.migrateSchema)(value, [], migration),
|
|
108
|
+
segment: () => (0, definitions_1.migrateSegment)(value, inSet, migration),
|
|
109
|
+
feature: () => (0, definitions_1.migrateFeature)(value, inSet, migration),
|
|
110
|
+
variable: () => (0, definitions_1.migrateVariable)(value, inSet, migration),
|
|
111
|
+
target: () => value,
|
|
112
|
+
test: () => (0, definitions_1.migrateTest)(value, migration),
|
|
113
|
+
};
|
|
114
|
+
migrated.set(file, { value: kinds[file.kind](), migration });
|
|
115
|
+
}
|
|
116
|
+
for (const file of project.files) {
|
|
117
|
+
const entry = migrated.get(file);
|
|
118
|
+
if (!entry)
|
|
119
|
+
continue;
|
|
120
|
+
const { source, value } = read.get(file);
|
|
121
|
+
const { migration } = entry;
|
|
122
|
+
const notes = migration.notes.map((note) => ({
|
|
123
|
+
path: note.path.join("."),
|
|
124
|
+
message: note.message,
|
|
125
|
+
link: link(note.section),
|
|
126
|
+
}));
|
|
127
|
+
if (project.nestedAttributes.includes(file.filePath)) {
|
|
128
|
+
notes.push({
|
|
129
|
+
path: "",
|
|
130
|
+
message: "v4 reads attribute files only directly in the attributes directory, without dots in their names. Move or rename it, or declare an object attribute.",
|
|
131
|
+
link: link("attribute-directories"),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
if ((0, lines_1.same)(entry.value, value)) {
|
|
135
|
+
report(file, { changes: [], notes });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const output = (0, write_1.writeDefinition)(source, value, entry.value, migration.renames, parser);
|
|
140
|
+
const changes = [...migration.changes];
|
|
141
|
+
const diff = options.diff
|
|
142
|
+
? (0, diff_1.unifiedDiff)(relative(file.filePath), relative(file.filePath), source, output)
|
|
143
|
+
: undefined;
|
|
144
|
+
if (write)
|
|
145
|
+
fs.writeFileSync(file.filePath, output);
|
|
146
|
+
result.migrated++;
|
|
147
|
+
report(file, { changes, notes, ...(diff ? { diff } : {}) });
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
const message = error instanceof write_1.WriteError
|
|
151
|
+
? `${error.message} Migrate it by hand.`
|
|
152
|
+
: error.message;
|
|
153
|
+
report(file, { changes: [...migration.changes], notes, error: message });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const { config } = project;
|
|
157
|
+
if (config.migrated !== config.source) {
|
|
158
|
+
const diff = options.diff
|
|
159
|
+
? (0, diff_1.unifiedDiff)(relative(config.filePath), relative(config.filePath), config.source, config.migrated)
|
|
160
|
+
: undefined;
|
|
161
|
+
if (write)
|
|
162
|
+
fs.writeFileSync(config.filePath, config.migrated);
|
|
163
|
+
result.files.push({
|
|
164
|
+
filePath: relative(config.filePath),
|
|
165
|
+
changes: config.changes,
|
|
166
|
+
notes: [],
|
|
167
|
+
...(diff ? { diff } : {}),
|
|
168
|
+
});
|
|
169
|
+
result.migrated++;
|
|
170
|
+
}
|
|
171
|
+
if (write) {
|
|
172
|
+
try {
|
|
173
|
+
(0, projectConfig_1.getProjectConfig)(rootDirectoryPath);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
result.files.push({
|
|
177
|
+
filePath: relative(config.filePath),
|
|
178
|
+
changes: [],
|
|
179
|
+
notes: [
|
|
180
|
+
{
|
|
181
|
+
path: "",
|
|
182
|
+
message: `v4 still cannot load the configuration: ${error.message}`,
|
|
183
|
+
link: link("changes-when-you-upgrade-the-cli"),
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
delete require.cache[require.resolve(config.filePath)];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
result.errors = result.files.filter((file) => file.error).length;
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=migrateProject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrateProject.js","sourceRoot":"","sources":["../../src/migrate/migrateProject.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,wCAmJC;AA7MD,4CAA8B;AAC9B,gDAAkC;AAElC,2DAA2D;AAC3D,yDAA8D;AAC9D,0CAA8C;AAC9C,4CAAwC;AACxC,+CASuB;AACvB,uCAA6D;AAC7D,mCAAsD;AAEtD,MAAM,KAAK,GAAG,8CAA8C,CAAC;AA8B7D,MAAM,IAAI,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,OAAO,EAAE,CAAC;AACxD,M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,CAAC;gBAAS,CAAC;YACT,OAAO,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;QACzD,CAAC;IACH,CAAC;IAED,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC;IACjE,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type CustomParser } from "@featurevisor/parsers";
|
|
2
|
+
import { type Kind } from "../rewrite/files";
|
|
3
|
+
/**
|
|
4
|
+
* A v3 project as migration reads it. The v4 configuration loader rejects v3 settings, so this
|
|
5
|
+
* reads featurevisor.config.js directly and finds the definition files by their directories.
|
|
6
|
+
*/
|
|
7
|
+
export interface DefinitionFile {
|
|
8
|
+
kind: Kind;
|
|
9
|
+
/** The Set it belongs to, in a project with Sets */
|
|
10
|
+
set?: string;
|
|
11
|
+
filePath: string;
|
|
12
|
+
}
|
|
13
|
+
export interface Project {
|
|
14
|
+
rootDirectoryPath: string;
|
|
15
|
+
parser: CustomParser;
|
|
16
|
+
files: DefinitionFile[];
|
|
17
|
+
config: {
|
|
18
|
+
filePath: string;
|
|
19
|
+
source: string;
|
|
20
|
+
migrated: string;
|
|
21
|
+
changes: string[];
|
|
22
|
+
};
|
|
23
|
+
/** Problems that stop migration before anything is written */
|
|
24
|
+
blockers: {
|
|
25
|
+
message: string;
|
|
26
|
+
section: string;
|
|
27
|
+
}[];
|
|
28
|
+
/** Attribute files v4 rejects for where they are */
|
|
29
|
+
nestedAttributes: string[];
|
|
30
|
+
}
|
|
31
|
+
export declare function readProject(rootDirectoryPath: string): Project;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.readProject = readProject;
|
|
37
|
+
const fs = __importStar(require("node:fs"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const parsers_1 = require("@featurevisor/parsers");
|
|
40
|
+
const projectConfig_1 = require("../config/projectConfig");
|
|
41
|
+
const files_1 = require("../rewrite/files");
|
|
42
|
+
function requireFresh(filePath) {
|
|
43
|
+
const resolved = require.resolve(filePath);
|
|
44
|
+
delete require.cache[resolved];
|
|
45
|
+
try {
|
|
46
|
+
return require(resolved);
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
delete require.cache[resolved];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** The config text with the v3 settings v4 renamed or rejects rewritten, when that is safe */
|
|
53
|
+
function migrateConfigSource(source) {
|
|
54
|
+
const changes = [];
|
|
55
|
+
let migrated = source;
|
|
56
|
+
if (/\ballowFeatureAndGlobalVariableKeyCollisions\b/.test(migrated)) {
|
|
57
|
+
migrated = migrated.replace(/\ballowFeatureAndGlobalVariableKeyCollisions\b/g, "allowFeatureAndVariableKeyCollisions");
|
|
58
|
+
changes.push("allowFeatureAndVariableKeyCollisions");
|
|
59
|
+
}
|
|
60
|
+
const emptyEnvironments = /^[ \t]*environments\s*:\s*\[\s*\]\s*,?[ \t]*\r?\n/m;
|
|
61
|
+
if (emptyEnvironments.test(migrated)) {
|
|
62
|
+
migrated = migrated.replace(emptyEnvironments, "");
|
|
63
|
+
changes.push("environments");
|
|
64
|
+
}
|
|
65
|
+
return { migrated, changes };
|
|
66
|
+
}
|
|
67
|
+
function readProject(rootDirectoryPath) {
|
|
68
|
+
const configPath = path.join(rootDirectoryPath, projectConfig_1.CONFIG_MODULE_NAME);
|
|
69
|
+
const source = fs.readFileSync(configPath, "utf8");
|
|
70
|
+
const config = requireFresh(configPath);
|
|
71
|
+
const blockers = [];
|
|
72
|
+
let parser = parsers_1.parsers.yml;
|
|
73
|
+
if (typeof config.parser === "string") {
|
|
74
|
+
if (parsers_1.parsers[config.parser])
|
|
75
|
+
parser = parsers_1.parsers[config.parser];
|
|
76
|
+
else
|
|
77
|
+
blockers.push({
|
|
78
|
+
message: `Unknown parser: ${config.parser}.`,
|
|
79
|
+
section: "upgrade-the-project",
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else if (config.parser) {
|
|
83
|
+
parser = config.parser;
|
|
84
|
+
if (typeof parser.stringify !== "function") {
|
|
85
|
+
blockers.push({
|
|
86
|
+
message: "The custom parser has no stringify function, which v4 needs to write definitions, and migration to write them. Add one first.",
|
|
87
|
+
section: "custom-parsers-must-support-writing",
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const directory = (key, name) => typeof config[key] === "string"
|
|
92
|
+
? config[key].replace(projectConfig_1.ROOT_DIR_PLACEHOLDER, rootDirectoryPath)
|
|
93
|
+
: path.join(rootDirectoryPath, name);
|
|
94
|
+
// the configured directories, or each Set's own
|
|
95
|
+
const roots = [];
|
|
96
|
+
if (config.sets === true) {
|
|
97
|
+
const setsDirectory = directory("setsDirectoryPath", projectConfig_1.SETS_DIRECTORY_NAME);
|
|
98
|
+
const sets = fs.existsSync(setsDirectory)
|
|
99
|
+
? fs
|
|
100
|
+
.readdirSync(setsDirectory, { withFileTypes: true })
|
|
101
|
+
.filter((entry) => entry.isDirectory())
|
|
102
|
+
.map((entry) => entry.name)
|
|
103
|
+
.sort()
|
|
104
|
+
: [];
|
|
105
|
+
for (const set of sets) {
|
|
106
|
+
roots.push({
|
|
107
|
+
set,
|
|
108
|
+
directory: (_, name) => path.join(setsDirectory, set, name),
|
|
109
|
+
groups: path.join(setsDirectory, set, "groups"),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
roots.push({ directory, groups: path.join(rootDirectoryPath, "groups") });
|
|
115
|
+
}
|
|
116
|
+
const groups = roots
|
|
117
|
+
.flatMap((root) => (0, files_1.filesIn)(root.groups, parser.extension))
|
|
118
|
+
.map((group) => path.relative(rootDirectoryPath, group));
|
|
119
|
+
if (typeof config.groupsDirectoryPath === "string") {
|
|
120
|
+
groups.push(`groupsDirectoryPath in ${projectConfig_1.CONFIG_MODULE_NAME}`);
|
|
121
|
+
}
|
|
122
|
+
if (groups.length > 0) {
|
|
123
|
+
blockers.push({
|
|
124
|
+
message: `Groups were removed in v4, and this project still has them: ${groups.join(", ")}. Convert them with the v3 CLI and publish their datafiles first.`,
|
|
125
|
+
section: "groups-removed",
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const files = [];
|
|
129
|
+
const nestedAttributes = [];
|
|
130
|
+
for (const root of roots) {
|
|
131
|
+
for (const [kind, key, name] of files_1.KINDS) {
|
|
132
|
+
const base = root.directory(key, name);
|
|
133
|
+
for (const filePath of (0, files_1.filesIn)(base, parser.extension)) {
|
|
134
|
+
files.push({ kind, ...(root.set ? { set: root.set } : {}), filePath });
|
|
135
|
+
const relative = path.relative(base, filePath);
|
|
136
|
+
if (kind === "attribute" &&
|
|
137
|
+
(relative.includes(path.sep) ||
|
|
138
|
+
path.basename(relative, `.${parser.extension}`).includes("."))) {
|
|
139
|
+
nestedAttributes.push(filePath);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
rootDirectoryPath,
|
|
146
|
+
parser,
|
|
147
|
+
files,
|
|
148
|
+
config: { filePath: configPath, source, ...migrateConfigSource(source) },
|
|
149
|
+
blockers,
|
|
150
|
+
nestedAttributes,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=project.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"project.js","sourceRoot":"","sources":["../../src/migrate/project.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,kCAgGC;AAhKD,4CAA8B;AAC9B,gDAAkC;AAElC,mDAAmE;AAEnE,2DAIiC;AACjC,4CAA6D;AAyB7D,SAAS,YAAY,CAAC,QAAgB;IACpC,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3C,OAAO,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC/B,IAAI,CAAC;QACH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 = require("../error");
|
|
5
|
+
const cliFormat_1 = require("../tester/cliFormat");
|
|
6
|
+
const migrateProject_1 = require("./migrateProject");
|
|
7
|
+
function printResult(result, options) {
|
|
8
|
+
const write = !options.check && !options.diff;
|
|
9
|
+
if (result.blockers.length > 0) {
|
|
10
|
+
for (const blocker of result.blockers) {
|
|
11
|
+
console.log(cliFormat_1.CLI_FORMAT_RED, blocker.message);
|
|
12
|
+
console.log(cliFormat_1.CLI_FORMAT_DIM, ` ${blocker.link}`);
|
|
13
|
+
}
|
|
14
|
+
console.log("");
|
|
15
|
+
console.log(cliFormat_1.CLI_FORMAT_RED, "Nothing was migrated");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const file of result.files) {
|
|
19
|
+
if (file.error) {
|
|
20
|
+
console.log(cliFormat_1.CLI_FORMAT_RED, file.filePath);
|
|
21
|
+
console.log(cliFormat_1.CLI_FORMAT_RED, ` => Error: ${file.error}`);
|
|
22
|
+
}
|
|
23
|
+
else if (file.changes.length > 0) {
|
|
24
|
+
if (options.diff && file.diff)
|
|
25
|
+
process.stdout.write(file.diff);
|
|
26
|
+
else
|
|
27
|
+
console.log(`${write ? "Migrated " : ""}${file.filePath}: ${file.changes.join(", ")}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const notes = result.files.filter((file) => file.notes.length > 0);
|
|
31
|
+
if (notes.length > 0) {
|
|
32
|
+
console.log("");
|
|
33
|
+
console.log(cliFormat_1.CLI_FORMAT_YELLOW, "Needs you:");
|
|
34
|
+
for (const file of notes) {
|
|
35
|
+
for (const note of file.notes) {
|
|
36
|
+
console.log(` ${file.filePath}${note.path ? ` (${note.path})` : ""}`);
|
|
37
|
+
console.log(` ${note.message}`);
|
|
38
|
+
console.log(cliFormat_1.CLI_FORMAT_DIM, ` ${note.link}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const files = (count) => `${count} ${count === 1 ? "file" : "files"}`;
|
|
43
|
+
console.log("");
|
|
44
|
+
if (result.errors > 0)
|
|
45
|
+
console.log(cliFormat_1.CLI_FORMAT_RED, `${files(result.errors)} could not be migrated`);
|
|
46
|
+
if (result.migrated === 0) {
|
|
47
|
+
console.log(cliFormat_1.CLI_FORMAT_GREEN, "✔ Nothing left to migrate");
|
|
48
|
+
}
|
|
49
|
+
else if (write) {
|
|
50
|
+
console.log(cliFormat_1.CLI_FORMAT_GREEN, `✔ Migrated ${files(result.migrated)}`);
|
|
51
|
+
console.log("Next: run lint, test and build, and review the changes before you commit them.");
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const verb = result.migrated === 1 ? "needs" : "need";
|
|
55
|
+
console.log(cliFormat_1.CLI_FORMAT_YELLOW, `${files(result.migrated)} ${verb} migrating. Run: npx featurevisor migrate`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** The command: 0 when done, 1 when --check finds files to migrate, and 2 on errors */
|
|
59
|
+
async function runMigration(rootDirectoryPath, parsed) {
|
|
60
|
+
const options = { check: parsed.check === true, diff: parsed.diff === true };
|
|
61
|
+
const json = parsed.json === true;
|
|
62
|
+
try {
|
|
63
|
+
const result = (0, migrateProject_1.migrateProject)(rootDirectoryPath, options);
|
|
64
|
+
if (json) {
|
|
65
|
+
console.log(parsed.pretty ? JSON.stringify(result, null, 2) : JSON.stringify(result));
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
printResult(result, options);
|
|
69
|
+
}
|
|
70
|
+
if (result.blockers.length > 0 || result.errors > 0)
|
|
71
|
+
return 2;
|
|
72
|
+
if (options.check && result.migrated > 0)
|
|
73
|
+
return 1;
|
|
74
|
+
return 0;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error((0, error_1.formatFeaturevisorCLIError)(error, { json, pretty: parsed.pretty === true }));
|
|
78
|
+
return 2;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1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;QACxF,CAAC;aAAM,CAAC;YACN,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC;YAAE,OAAO,CAAC,CAAC;QAC9D,IAAI,OAAO,CAAC,KAAK,IAAI,MAAM,CAAC,QAAQ,GAAG,CAAC;YAAE,OAAO,CAAC,CAAC;QACnD,OAAO,CAAC,CAAC;IACX,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,IAAA,kCAA0B,EAAC,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC;QAC3F,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type CustomParser } from "@featurevisor/parsers";
|
|
2
|
+
import { type Rename } from "./definitions";
|
|
3
|
+
export declare class WriteError extends Error {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The file's new text, with the file's own line endings and byte order mark. Throws a
|
|
7
|
+
* WriteError when the text would not parse to the migrated value or would lose a comment, so
|
|
8
|
+
* nothing is written that migration did not mean.
|
|
9
|
+
*/
|
|
10
|
+
export declare function writeDefinition(file: string, before: unknown, after: unknown, renames: Rename[], parser: CustomParser): string;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WriteError = void 0;
|
|
4
|
+
exports.writeDefinition = writeDefinition;
|
|
5
|
+
const node_util_1 = require("node:util");
|
|
6
|
+
const parsers_1 = require("@featurevisor/parsers");
|
|
7
|
+
const yaml_1 = require("yaml");
|
|
8
|
+
const lines_1 = require("../rewrite/lines");
|
|
9
|
+
const definitions_1 = require("./definitions");
|
|
10
|
+
/**
|
|
11
|
+
* Writes a migrated value back to its file. The new text is rendered from the value, then only
|
|
12
|
+
* its changes are merged into the file's own text, so every line migration did not touch stays
|
|
13
|
+
* exactly as written.
|
|
14
|
+
*/
|
|
15
|
+
// YAML
|
|
16
|
+
function keyOf(pair) {
|
|
17
|
+
return (0, yaml_1.isScalar)(pair.key) ? String(pair.key.value) : undefined;
|
|
18
|
+
}
|
|
19
|
+
function commentsIn(node) {
|
|
20
|
+
const comments = [];
|
|
21
|
+
const add = (comment) => comment && comments.push(comment);
|
|
22
|
+
if (!node || typeof node !== "object")
|
|
23
|
+
return comments;
|
|
24
|
+
(0, yaml_1.visit)(node, {
|
|
25
|
+
Node(_, child) {
|
|
26
|
+
add(child.commentBefore);
|
|
27
|
+
add(child.comment);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
return comments;
|
|
31
|
+
}
|
|
32
|
+
function addCommentBefore(node, comments) {
|
|
33
|
+
if (!node || comments.length === 0)
|
|
34
|
+
return;
|
|
35
|
+
node.commentBefore = [node.commentBefore, ...comments].filter(Boolean).join("\n");
|
|
36
|
+
}
|
|
37
|
+
/** A new node for a value, which keeps the comments and blank line of the one it replaces */
|
|
38
|
+
function replace(doc, old, value) {
|
|
39
|
+
const node = doc.createNode(value);
|
|
40
|
+
if (old && typeof old === "object") {
|
|
41
|
+
const previous = old;
|
|
42
|
+
node.spaceBefore = previous.spaceBefore;
|
|
43
|
+
if ((0, yaml_1.isScalar)(previous)) {
|
|
44
|
+
node.commentBefore = previous.commentBefore;
|
|
45
|
+
node.comment = previous.comment;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
addCommentBefore(node, commentsIn(previous));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return node;
|
|
52
|
+
}
|
|
53
|
+
const samePath = (a, b) => a.length === b.length && a.every((key, index) => key === b[index]);
|
|
54
|
+
/** Updates the node for `value` in place, keeping every node whose value is the same */
|
|
55
|
+
function sync(doc, node, value, path, renames) {
|
|
56
|
+
if ((0, yaml_1.isAlias)(node)) {
|
|
57
|
+
return (0, node_util_1.isDeepStrictEqual)(node.resolve(doc)?.toJS(doc), value)
|
|
58
|
+
? node
|
|
59
|
+
: replace(doc, node, value);
|
|
60
|
+
}
|
|
61
|
+
if ((0, definitions_1.isPlain)(value)) {
|
|
62
|
+
if (!(0, yaml_1.isMap)(node))
|
|
63
|
+
return replace(doc, node, value);
|
|
64
|
+
const pairs = node.items;
|
|
65
|
+
const used = new Set();
|
|
66
|
+
const items = [];
|
|
67
|
+
for (const [key, item] of Object.entries(value)) {
|
|
68
|
+
let pair = pairs.find((candidate) => !used.has(candidate) && keyOf(candidate) === key);
|
|
69
|
+
const rename = renames.find((entry) => samePath(entry.path, path) && entry.to === key);
|
|
70
|
+
if (!pair && rename) {
|
|
71
|
+
pair = rename.from
|
|
72
|
+
.map((from) => pairs.find((candidate) => !used.has(candidate) && keyOf(candidate) === from))
|
|
73
|
+
.find(Boolean);
|
|
74
|
+
if (pair)
|
|
75
|
+
pair.key.value = key;
|
|
76
|
+
}
|
|
77
|
+
if (pair) {
|
|
78
|
+
used.add(pair);
|
|
79
|
+
const previous = pair.value;
|
|
80
|
+
pair.value = sync(doc, previous, item, [...path, key], renames);
|
|
81
|
+
// a comment after a value that is now a block list or map stays on the key's line
|
|
82
|
+
const next = pair.value;
|
|
83
|
+
if ((0, yaml_1.isScalar)(previous) && previous.comment && ((0, yaml_1.isMap)(next) || (0, yaml_1.isSeq)(next)) && !next.flow) {
|
|
84
|
+
pair.key.comment = next.comment;
|
|
85
|
+
next.comment = undefined;
|
|
86
|
+
}
|
|
87
|
+
items.push(pair);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const added = doc.createPair(key, item);
|
|
91
|
+
// a key added to a map whose keys are spaced is spaced too
|
|
92
|
+
const spaced = pairs.slice(1).some((other) => other.key?.spaceBefore);
|
|
93
|
+
if (spaced && items.length > 0)
|
|
94
|
+
added.key.spaceBefore = true;
|
|
95
|
+
items.push(added);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// the comments of keys merged away go to the key that took them in, or the first one
|
|
99
|
+
for (const pair of pairs) {
|
|
100
|
+
if (used.has(pair))
|
|
101
|
+
continue;
|
|
102
|
+
const rename = renames.find((entry) => samePath(entry.path, path) && entry.from.includes(keyOf(pair) ?? ""));
|
|
103
|
+
const target = items.find((item) => keyOf(item) === rename?.to) || items[0];
|
|
104
|
+
addCommentBefore(target?.key, commentsIn(pair));
|
|
105
|
+
}
|
|
106
|
+
node.items = items;
|
|
107
|
+
return node;
|
|
108
|
+
}
|
|
109
|
+
if (Array.isArray(value)) {
|
|
110
|
+
if (!(0, yaml_1.isSeq)(node))
|
|
111
|
+
return replace(doc, node, value);
|
|
112
|
+
const items = node.items;
|
|
113
|
+
node.items = value.map((item, index) => index < items.length
|
|
114
|
+
? sync(doc, items[index], item, [...path, index], renames)
|
|
115
|
+
: doc.createNode(item));
|
|
116
|
+
for (const dropped of items.slice(value.length)) {
|
|
117
|
+
node.comment = [node.comment, ...commentsIn(dropped)].filter(Boolean).join("\n");
|
|
118
|
+
}
|
|
119
|
+
return node;
|
|
120
|
+
}
|
|
121
|
+
if ((0, yaml_1.isScalar)(node) && node.value === value)
|
|
122
|
+
return node;
|
|
123
|
+
return replace(doc, node, value);
|
|
124
|
+
}
|
|
125
|
+
function renderYaml(source, value, renames) {
|
|
126
|
+
const options = (0, lines_1.yamlOptions)(source);
|
|
127
|
+
const doc = (0, yaml_1.parseDocument)(source);
|
|
128
|
+
const base = doc.toString(options);
|
|
129
|
+
doc.contents = sync(doc, doc.contents, value, [], renames);
|
|
130
|
+
return { base, changed: doc.toString(options) };
|
|
131
|
+
}
|
|
132
|
+
// Any parser
|
|
133
|
+
class WriteError extends Error {
|
|
134
|
+
}
|
|
135
|
+
exports.WriteError = WriteError;
|
|
136
|
+
/**
|
|
137
|
+
* The file's new text, with the file's own line endings and byte order mark. Throws a
|
|
138
|
+
* WriteError when the text would not parse to the migrated value or would lose a comment, so
|
|
139
|
+
* nothing is written that migration did not mean.
|
|
140
|
+
*/
|
|
141
|
+
function writeDefinition(file, before, after, renames, parser) {
|
|
142
|
+
const result = (0, lines_1.rewriteFile)(file, parser, after, (text) => parser === parsers_1.parsers.yml
|
|
143
|
+
? renderYaml(text, after, renames)
|
|
144
|
+
: (0, lines_1.renderValues)(parser, before, after), { keepComments: true });
|
|
145
|
+
if (!result) {
|
|
146
|
+
throw new WriteError("The migrated file would not keep every comment, or would not read back as migrated.");
|
|
147
|
+
}
|
|
148
|
+
return result.text;
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=write.js.map
|