Repository navigation
Expand file tree
/
Copy pathgulpfile.ts
More file actions
139 lines (120 loc) · 4.06 KB
/
Copy pathgulpfile.ts
File metadata and controls
139 lines (120 loc) · 4.06 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
import { exec } from "child_process"
import fs from "fs/promises"
import gulp from "gulp"
import rename from "gulp-rename"
async function execAsync(command: string) {
return new Promise<void>((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (stderr.trim().length) {
console.error(stderr)
}
if (stdout.trim().length) {
console.log(stdout)
}
if (error) {
console.error(
`Command "${command}" failed with exit code ${error.code}.`,
)
reject(error)
} else {
resolve()
}
})
})
}
// -------------------------------------------------------------------------
// Packaging for browser
// -------------------------------------------------------------------------
function browserCopySources() {
return gulp
.src([
"./src/**/*.ts",
"!./src/commands/*.ts",
"!./src/cli.ts",
"!./src/typeorm.ts",
"!./src/typeorm-model-shim.ts",
])
.pipe(gulp.dest("./build/browser/src"))
}
function browserCopyTemplates() {
return gulp
.src("./src/platform/*.template")
.pipe(
rename((p) => {
p.extname = ".ts"
}),
)
.pipe(gulp.dest("./build/browser/src/platform"))
}
async function browserCompile() {
await execAsync("tsc -p tsconfig.browser.json")
}
function browserCopyShims() {
return gulp
.src([
"./extra/typeorm-model-shim.js",
"./extra/typeorm-class-transformer-shim.js",
])
.pipe(gulp.dest("./build/package"))
}
// -------------------------------------------------------------------------
// Packaging for Node.js
// -------------------------------------------------------------------------
async function nodeCompile() {
await execAsync("tsc -p tsconfig.node.json")
}
async function nodeCreateEsmIndex() {
const buildDir = "./build/package"
const cjsIndex = require(`${buildDir}/index.js`)
const cjsKeys = Object.keys(cjsIndex).filter(
(key) => key !== "default" && !key.startsWith("__"),
)
const indexMjsContent =
'import TypeORM from "./index.js";\n' +
`const {\n ${cjsKeys.join(",\n ")}\n} = TypeORM;\n` +
`export {\n ${cjsKeys.join(",\n ")}\n};\n` +
"export default TypeORM;\n"
await fs.writeFile(`${buildDir}/index.mjs`, indexMjsContent, "utf8")
}
// -------------------------------------------------------------------------
// Packaging
// -------------------------------------------------------------------------
async function copyPackageFile() {
const pkg = JSON.parse(await fs.readFile("./package.json", "utf8"))
// Preserve devDependencies used by `typeorm init` to generate project package.json
const initDevDeps = ["@types/node", "ts-node", "typescript"]
pkg.devDependencies = Object.fromEntries(
initDevDeps
.filter((dep) => dep in (pkg.devDependencies ?? {}))
.map((dep) => [dep, pkg.devDependencies[dep]]),
)
delete pkg.devEngines
delete pkg.packageManager
delete pkg.pnpm
delete pkg.scripts
await fs.writeFile(
"./build/package/package.json",
JSON.stringify(pkg, null, 2) + "\n",
)
}
function copyReadme() {
return gulp.src("./README.md").pipe(gulp.dest("./build/package"))
}
// -------------------------------------------------------------------------
// Tasks
// -------------------------------------------------------------------------
gulp.task(
"package",
gulp.series(
() => fs.rm("./build", { recursive: true, force: true }),
gulp.parallel(browserCopySources, browserCopyTemplates),
gulp.parallel(nodeCompile, browserCompile),
gulp.parallel(
nodeCreateEsmIndex,
copyPackageFile,
copyReadme,
browserCopyShims,
),
),
)
gulp.task("clean", () => fs.rm("./build", { recursive: true, force: true }))