-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathconfig.ts
More file actions
182 lines (160 loc) · 5.02 KB
/
config.ts
File metadata and controls
182 lines (160 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* config.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*
*/
import { join } from "../../../src/deno_ral/path.ts";
import { info } from "../../../src/deno_ral/log.ts";
import { getEnv } from "../util/utils.ts";
import { os as platformOs } from "../../../src/deno_ral/platform.ts"
import * as Pandoc from "../../../src/core/pandoc/json.ts";
import { fromObjects } from "../../../src/core/pandoc/table.ts";
// The core configuration for the packaging process
export interface Configuration extends PlatformConfiguration {
productName: string;
version: string;
importmap: string;
directoryInfo: DirectoryInfo;
}
export interface PlatformConfiguration {
os: "windows" | "linux" | "darwin";
arch: "x86_64" | "aarch64";
}
// Directories avaialable for step
export interface DirectoryInfo {
root: string;
src: string;
pkg: string;
dist: string;
share: string;
bin: string;
out: string;
tools: string;
pkgWorking: {
root: string;
bin: string;
share: string;
};
}
export const kValidOS = ["windows", "linux", "darwin"];
export const kValidArch = ["x86_64", "aarch64"];
// Read the configuration fromt the environment
export function readConfiguration(
version?: string,
os?: "windows" | "linux" | "darwin",
arch?: "x86_64" | "aarch64",
): Configuration {
const productName = getEnv("QUARTO_NAME");
version = version || getEnv("QUARTO_VERSION");
const root = getEnv("QUARTO_ROOT");
const src = getEnv("QUARTO_SRC_PATH");
const pkg = getEnv("QUARTO_PACKAGE_PATH") || "package";
const out = join(pkg, getEnv("QUARTO_OUT_DIR") || "out");
const dist = getEnv("QUARTO_DIST_PATH") || "dist";
const shareName = getEnv("QUARTO_SHARE_DIR") || "share";
const share = getEnv("QUARTO_SHARE_PATH") ||
join(dist, shareName);
const binName = getEnv("QUARTO_BIN_DIR") || "bin";
const bin = getEnv("QUARTO_BIN_PATH") ||
join(dist, binName);
const pkgWorkingBase = join(pkg, "pkg-working");
const toolsName = getEnv("QUARTO_TOOLS_NAME", "tools");
const tools = getEnv("QUARTO_TOOLS_PATH", join(root, toolsName));
const directoryInfo = {
root,
pkg,
dist,
share,
src,
out,
bin,
tools,
pkgWorking: {
root: pkgWorkingBase,
bin: join(pkgWorkingBase, binName),
share: join(pkgWorkingBase, shareName),
},
};
const cmdOs = os || getEnv("QUARTO_OS", platformOs);
if (!kValidOS.includes(cmdOs)) {
throw new Error(
`Invalid OS ${os} provided. Please use one of ${kValidOS.join(",")}`,
);
}
const cmdArch = arch || getEnv("QUARTO_OS", Deno.build.arch);
if (!kValidArch.includes(cmdArch)) {
throw new Error(
`Invalid arch ${arch} provided. Please use one of ${
kValidArch.join(",")
}`,
);
}
const importmap = join(src, "import_map.json");
return {
productName,
version,
importmap,
directoryInfo,
os: cmdOs as "windows" | "linux" | "darwin",
arch: cmdArch as "x86_64" | "aarch64",
};
}
export function configurationAST(config: Configuration): Pandoc.Block[] {
const makeObject = (
obj: {
key: string,
value: string
},
) => ({
"key": [Pandoc.plain([Pandoc.str(obj.key)])],
"value": [Pandoc.plain([Pandoc.code(obj.value)])]
});
const configTable = fromObjects([
{key: "OS", value: config.os},
{key: "Arch", value: config.arch},
{key: "Version", value: config.version},
{key: "Cwd", value: Deno.cwd()},
{key: "Package folder (build source)", value: config.directoryInfo.pkg},
{key: "Dist folder (output folder)", value: config.directoryInfo.dist},
{key: "Bin folder", value: config.directoryInfo.bin},
{key: "Share folder", value: config.directoryInfo.share},
{key: "Package working folder", value: config.directoryInfo.pkgWorking.root},
].map(makeObject), undefined, [
Pandoc.colspec("AlignLeft", {t: "ColWidth", c: 0.3}),
Pandoc.colspec("AlignLeft", {t: "ColWidth", c: 0.6}),
]);
return [
Pandoc.header(2, [Pandoc.str("Configuration:")]),
configTable,
];
}
export function printConfiguration(config: Configuration) {
info("");
info("******************************************");
info("Configuration:");
info(` - OS: ${config.os}`);
info(` - Arch: ${config.arch}`);
info(` - Version: ${config.version}`);
info(` - Cwd: ${Deno.cwd()}`);
info(` - Directory configuration:`);
info(
` - Package folder (build source): ${config.directoryInfo.pkg}`,
);
info(` - Dist folder (output folder): ${config.directoryInfo.dist}`);
info(` - bin folder: ${config.directoryInfo.bin}`);
info(` - share folder: ${config.directoryInfo.share}`);
info(` - Package working folder: ${config.directoryInfo.pkgWorking.root}`);
info("");
info("******************************************");
info("");
}
// Utility that provides a working directory and cleans it up
export async function withWorkingDir(fn: (wkDir: string) => Promise<void>) {
const workingDir = Deno.makeTempDirSync();
try {
await fn(workingDir);
} finally {
Deno.removeSync(workingDir, { recursive: true });
}
}