95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export function loadDepartmentConfig(root, department) {
|
|
const departmentsPath = path.join(root, 'departments.json');
|
|
const departments = JSON.parse(fs.readFileSync(departmentsPath, 'utf8'));
|
|
const config = departments[department];
|
|
|
|
if (!config) {
|
|
throw new Error(`Unknown department: ${department}`);
|
|
}
|
|
|
|
return {
|
|
config,
|
|
scriptIds: scriptIdsFromConfig(config, department),
|
|
};
|
|
}
|
|
|
|
export function scriptIdsFromConfig(config, department = 'department') {
|
|
const hasScriptIds = Object.prototype.hasOwnProperty.call(config, 'scriptIds');
|
|
const hasScriptId = Object.prototype.hasOwnProperty.call(config, 'scriptId');
|
|
|
|
if (hasScriptIds && hasScriptId) {
|
|
throw new Error(
|
|
`${department}: use either scriptIds or scriptId, not both`
|
|
);
|
|
}
|
|
|
|
if (!hasScriptIds && !hasScriptId) {
|
|
throw new Error(
|
|
`${department}: missing scriptIds. Set a non-empty array of Apps Script project ids`
|
|
);
|
|
}
|
|
|
|
const rawIds = hasScriptIds ? config.scriptIds : config.scriptId;
|
|
const label = hasScriptIds ? 'scriptIds' : 'scriptId';
|
|
|
|
if (hasScriptId && typeof rawIds === 'string') {
|
|
return dedupeScriptIds([rawIds], department, label);
|
|
}
|
|
|
|
if (!Array.isArray(rawIds)) {
|
|
throw new Error(
|
|
`${department}: ${label} must be ${
|
|
hasScriptIds ? 'an array of script ids' : 'a script id or an array of script ids'
|
|
}`
|
|
);
|
|
}
|
|
|
|
return dedupeScriptIds(rawIds, department, label);
|
|
}
|
|
|
|
export function writeClaspConfig(dir, scriptId) {
|
|
fs.writeFileSync(
|
|
path.join(dir, '.clasp.json'),
|
|
JSON.stringify(
|
|
{
|
|
scriptId,
|
|
rootDir: '.',
|
|
scriptExtensions: ['.js', '.gs'],
|
|
htmlExtensions: ['.html'],
|
|
jsonExtensions: ['.json'],
|
|
skipSubdirectories: true,
|
|
},
|
|
null,
|
|
2
|
|
)
|
|
);
|
|
}
|
|
|
|
function dedupeScriptIds(rawIds, department, label) {
|
|
const scriptIds = [];
|
|
const seen = new Set();
|
|
|
|
for (const rawId of rawIds) {
|
|
if (typeof rawId !== 'string' || rawId.trim() === '') {
|
|
throw new Error(`${department}: ${label} must contain non-empty strings`);
|
|
}
|
|
|
|
const scriptId = rawId.trim();
|
|
if (seen.has(scriptId)) {
|
|
continue;
|
|
}
|
|
|
|
seen.add(scriptId);
|
|
scriptIds.push(scriptId);
|
|
}
|
|
|
|
if (scriptIds.length === 0) {
|
|
throw new Error(`${department}: ${label} is empty`);
|
|
}
|
|
|
|
return scriptIds;
|
|
}
|