Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ initCommand.handler = async function() {
const {default: init} = await import("../../init/init.js");
const {default: path} = await import("node:path");
const {writeFile} = await import("node:fs/promises");
const {default: jsYaml} = await import("js-yaml");
const {dump} = await import("js-yaml");

const yamlPath = path.resolve("./ui5.yaml");
if (await exists(yamlPath)) {
throw new Error("Initialization not possible: ui5.yaml already exists");
}

const projectConfig = await init();
const yaml = jsYaml.dump(projectConfig, {quotingType: `"`});
const yaml = dump(projectConfig, {quoteStyle: "double"});

await writeFile(yamlPath, yaml);
process.stdout.write(`Wrote ui5.yaml to ${yamlPath}:`);
Expand Down
4 changes: 4 additions & 0 deletions lib/framework/ui5YamlSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {CORE_SCHEMA, mergeTag, timestampTag} from "js-yaml";

// Preserve merge keys and implicit timestamps from the js-yaml v4 default schema.
export default CORE_SCHEMA.withTags([mergeTag, timestampTag]);
6 changes: 4 additions & 2 deletions lib/framework/updateYaml.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import path from "node:path";
import {readFile, writeFile} from "node:fs/promises";
import {loadAll, dump} from "js-yaml";
import ui5YamlSchema from "./ui5YamlSchema.js";
import {fromYaml, getPosition, getValue, getKind} from "data-with-position";
import {getLogger} from "@ui5/logger";

const log = getLogger("cli:framework:updateYaml");

function getProjectYamlDocument({project, configFile, configPath}) {
const configs = loadAll(configFile, undefined, {
filename: configPath
filename: configPath,
schema: ui5YamlSchema
});

const projectDocumentIndex = configs.findIndex((config) => {
Expand Down Expand Up @@ -277,7 +279,7 @@ export default async function({project, configPathOverride, data}) {

// Validate content before writing
try {
loadAll(adoptedYaml);
loadAll(adoptedYaml, undefined, {schema: ui5YamlSchema});
} catch (err) {
const error = new Error("Failed to update YAML file: " + err.message);
error.name = "FrameworkUpdateYamlFailed";
Expand Down
55 changes: 50 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"chalk": "^5.6.2",
"data-with-position": "^0.5.0",
"import-local": "^3.2.0",
"js-yaml": "^4.3.2",
"js-yaml": "^5.4.3",
"open": "^11.0.4",
"pretty-hrtime": "^1.0.3",
"semver": "^7.8.5",
Expand Down
5 changes: 4 additions & 1 deletion test/lib/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ test.serial("Writes ui5.yaml to fs", async (t) => {

t.is(fsWriteFileStub.getCall(0).args[0], ui5YamlPath, "Passes yaml path to write the yaml file to");
t.is(fsWriteFileStub.getCall(0).args[1], ui5Yaml, "Passes yaml content to write to fs");
t.deepEqual(jsyamlDumpStub.getCall(0).args[1], {quotingType: `"`}, "Enforce usage of double quotes in yaml files");
t.deepEqual(
jsyamlDumpStub.getCall(0).args[1], {quoteStyle: "double"},
"Enforce usage of double quotes in yaml files"
);
});

test.serial("Error: throws if ui5.yaml already exists", async (t) => {
Expand Down
38 changes: 38 additions & 0 deletions test/lib/framework/updateYaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ framework:
`, "writeFile should be called with expected content");
});

test.serial("Should update document with merged metadata", async (t) => {
t.context.fsReadFileStub.resolves(`
---
projectDefaults: &projectDefaults
metadata:
name: my-project
<<: *projectDefaults
framework:
name: SAPUI5
version: 1.0.0
`);

await updateYaml({
project: {
getRootPath: () => "my-project",
getName: () => "my-project"
},
data: {
framework: {
name: "OpenUI5",
version: "1.76.0"
}
}
});

t.is(t.context.fsWriteFileStub.callCount, 1, "fs.writeFile should be called once");
t.is(t.context.fsWriteFileStub.getCall(0).args[1], `
---
projectDefaults: &projectDefaults
metadata:
name: my-project
<<: *projectDefaults
framework:
name: OpenUI5
version: "1.76.0"
`, "writeFile should be called with expected content");
});

test.serial("Should update first document", async (t) => {
t.context.fsReadFileStub.resolves(`
specVersion: "2.0"
Expand Down
Loading