-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsup.config.ts
116 lines (104 loc) · 3.49 KB
/
tsup.config.ts
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
import path from 'path';
import { dependencies } from './package.json';
import fs from 'fs-extra';
import pico from 'picocolors';
import { defineConfig } from 'tsup';
const { log } = console;
getTemplates();
export default defineConfig({
bundle: true,
clean: true,
entry: ['src/index.ts'],
external: Object.keys(dependencies),
format: ['esm'],
ignoreWatch: ['**/generated/**'],
async onSuccess() {
getTemplates();
return Promise.resolve();
},
platform: 'node',
});
/**
* Temporary workaround until esbuild supports glob imports.
*
* @see https://github.com/evanw/esbuild/pull/2508
*
* @returns The list of templates.
*/
function getTemplates(): string[] {
const frameworks = fs.readdirSync(path.join(__dirname, 'templates'));
let frameworksSrc = 'export const templateFrameworks = {';
let templatesSrc = 'export const templates = {';
for (const framework of frameworks) {
const frameworkPath = path.join(__dirname, 'templates', framework);
if (!fs.lstatSync(frameworkPath).isDirectory()) continue;
if (fs.existsSync(path.join(frameworkPath, '_meta.ts'))) {
frameworksSrc += `\n '${framework}': () => import('../../templates/${framework}/_meta'),`;
templatesSrc += `\n '${framework}': {`;
} else {
log(
pico.yellow(
[
`Unable to find \`_meta.ts\` for framework "${framework}".`,
'',
'Please make sure that you have a file named `_meta.ts` in the root of your framework folder.',
'',
'Example:',
'',
`${path.join(frameworkPath, '_meta.ts')}`,
'```',
"import { createTemplateFramework } from '../createTemplateFramework'",
'',
'export default createTemplateFramework({',
' name: "example-framework",',
' title: "Example Framework",',
' description: "An example framework",',
'})',
'```',
].join('\n')
)
);
}
const templates = fs.readdirSync(frameworkPath);
for (const template of templates) {
const templatePath = path.join(frameworkPath, template);
if (!fs.lstatSync(templatePath).isDirectory()) continue;
if (fs.existsSync(path.join(templatePath, '_meta.ts'))) {
templatesSrc += `\n '${template}': () => import('../../templates/${framework}/${template}/_meta'),`;
} else {
log(
pico.yellow(
[
`Unable to find \`_meta.ts\` for template "${template}".`,
'',
'Please make sure that you have a file named `_meta.ts` in the root of your template folder.',
'',
'Example:',
'',
`${path.join(templatePath, '_meta.ts')}`,
'```',
"import { createTemplate } from '../createTemplate'",
'',
'export default createTemplate({',
' name: "example-template",',
' title: "Example Template",',
' description: "An example template using Nori",',
'})',
'```',
].join('\n')
)
);
}
}
templatesSrc += '\n },';
}
templatesSrc += '\n}\n';
frameworksSrc += '\n}\n';
const generatedPath = path.join(__dirname, 'src/generated');
if (!fs.existsSync(generatedPath)) fs.mkdirSync(generatedPath);
fs.writeFileSync(
path.join(generatedPath, 'imports.ts'),
`${templatesSrc}\n${frameworksSrc}`
);
return frameworks;
}