Skip to content

Commit de96dd7

Browse files
authored
Merge pull request #14 from Araxeus/write-final-new-line-in-config-file-if-present-previously
2 parents 0be47d0 + b3bf490 commit de96dd7

File tree

2 files changed

+49
-58
lines changed

2 files changed

+49
-58
lines changed

lib/config.ts

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,37 @@ async function getConfigFile(folderPath: string): Promise<
5656
} & ConfigFileSettings)
5757
| null
5858
> {
59-
const tomlConfig = await findFirstFile(folderPath, ['vendor.toml']);
60-
if (tomlConfig) {
61-
return {
62-
format: 'toml',
63-
data: toml.parse(tomlConfig.data, {
64-
joiner: EOL,
65-
}) as ConfigFile,
66-
path: tomlConfig.path,
67-
indent: detectIndent(tomlConfig.data).indent || 2,
68-
};
69-
}
70-
71-
const ymlConfig = await findFirstFile(folderPath, [
59+
const config = await findFirstFile(folderPath, [
60+
'vendor.toml',
7261
'vendor.yml',
7362
'vendor.yaml',
74-
]);
75-
if (ymlConfig) {
76-
return {
77-
format: 'yml',
78-
data: yaml.parse(ymlConfig.data),
79-
path: ymlConfig.path,
80-
indent: detectIndent(ymlConfig.data).indent || 2,
81-
};
82-
}
83-
84-
const jsonConfig = await findFirstFile(folderPath, [
8563
'vendor.json',
8664
'package.json',
8765
]);
88-
if (jsonConfig) {
66+
if (config) {
67+
let data: ConfigFile;
68+
let format: 'toml' | 'yml' | 'json';
69+
if (config.path.endsWith('.toml')) {
70+
data = toml.parse(config.data, {
71+
joiner: EOL,
72+
}) as ConfigFile;
73+
format = 'toml';
74+
} else if (
75+
config.path.endsWith('.yml') ||
76+
config.path.endsWith('.yaml')
77+
) {
78+
data = yaml.parse(config.data);
79+
format = 'yml';
80+
} else {
81+
data = parseJson(config.data);
82+
format = 'json';
83+
}
8984
return {
90-
format: 'json',
91-
data: parseJson(jsonConfig.data),
92-
path: jsonConfig.path,
93-
indent: detectIndent(jsonConfig.data).indent || 2,
85+
format,
86+
data,
87+
path: config.path,
88+
indent: detectIndent(config.data).indent || 2,
89+
finalNewLine: config.data.match(/\r?\n$/)?.[0] || '',
9490
};
9591
}
9692

@@ -136,6 +132,7 @@ export async function getConfig(): Promise<VendorsOptions> {
136132
format: configFile.format,
137133
path: configFile.path,
138134
indent: configFile.indent,
135+
finalNewLine: configFile.finalNewLine,
139136
},
140137
};
141138

@@ -147,39 +144,32 @@ export async function writeConfig({
147144
configFileSettings,
148145
}: { configFile: ConfigFile; configFileSettings: ConfigFileSettings }) {
149146
const indent = configFileSettings.indent;
150-
switch (configFileSettings.format) {
151-
case 'toml':
147+
const stringify = {
148+
toml: (configFile: ConfigFile) => {
152149
Object.keys(configFile.vendorDependencies).forEach((key) => {
153150
if (configFile.vendorDependencies[key]) {
154151
configFile.vendorDependencies[key] = Section(
155152
configFile.vendorDependencies[key],
156153
);
157154
}
158155
});
159-
await writeFile(
160-
configFileSettings.path,
161-
// @ts-expect-error toml doesn't understand that the ConfigFile type is just an object
162-
toml.stringify(configFile, {
163-
newline: EOL,
164-
indent: configFileSettings.indent,
165-
newlineAround: 'section',
166-
}),
167-
);
168-
break;
169-
case 'yml':
170-
await writeFile(
171-
configFileSettings.path,
172-
yaml.stringify(configFile, {
173-
indent:
174-
typeof indent === 'number' ? indent : indent?.length,
175-
}),
176-
);
177-
break;
178-
case 'json':
179-
await writeFile(
180-
configFileSettings.path,
181-
JSON.stringify(configFile, null, indent),
182-
);
183-
break;
184-
}
156+
// @ts-expect-error toml doesn't understand that the ConfigFile type is just an object
157+
return toml.stringify(configFile, {
158+
newline: EOL,
159+
indent,
160+
newlineAround: 'section',
161+
});
162+
},
163+
yml: (configFile: ConfigFile) =>
164+
yaml.stringify(configFile, {
165+
indent: typeof indent === 'number' ? indent : indent?.length,
166+
}),
167+
json: (configFile: ConfigFile) =>
168+
JSON.stringify(configFile, null, indent),
169+
};
170+
const data = stringify[configFileSettings.format](configFile);
171+
await writeFile(
172+
configFileSettings.path,
173+
data + configFileSettings.finalNewLine,
174+
);
185175
}

lib/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export type ConfigFile = {
1414
export type ConfigFileSettings = {
1515
format: 'toml' | 'yml' | 'json';
1616
path: string;
17-
indent?: number | string;
17+
indent: number | string;
18+
finalNewLine: string;
1819
};
1920

2021
export type VendorConfig = {

0 commit comments

Comments
 (0)