-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.ts
138 lines (102 loc) · 4.73 KB
/
globals.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Built-In Modules
import { join, resolve } from "node:path";
import { dirname } from 'node:path';
import { existsSync, readFileSync } from "node:fs";
import { createRequire } from 'node:module';
import { removeDirectory } from './utils/files.js'
// External Packages
import * as yaml from 'js-yaml'
import dotenv from 'dotenv'
dotenv.config()
// Internal Imports
import { printFailure, printSubtle } from "./utils/formatting.js";
import { TargetType, WritableElectronBuilderConfig, universalTargetTypes, validDesktopTargets, validMobileTargets } from "./types.js";
// Dynamic Imports
export const chalk = import("chalk").then(m => m.default)
export const vite = import("vite")
const require = createRequire(import.meta.url);
const { version: electronVersion } = require('electron/package.json')
export { electronVersion }
export const globalWorkspacePath = '.commoners'
export const globalTempDir = join(globalWorkspacePath, '.temp')
const callbacks = []
export const onCleanup = (callback) => callbacks.push(callback)
export const cleanup = (code = 0) => {
callbacks.forEach(cb => {
if (!cb.called) cb(code)
cb.called = true // Prevent double-calling
})
}
const exitEvents = ['beforeExit', 'exit', 'SIGINT']
exitEvents.forEach(event => process.on(event, (code) => {
cleanup(code)
process.exit(code === 'SIGINT' ? 0 : code)
}))
export const handleTemporaryDirectories = async (tempDir = globalTempDir) => {
// NOTE: Ensure that the single temporary directory is not overwritten for different targets
if (existsSync(tempDir)) {
await printFailure('An active development build was detected for this project.')
await printSubtle('Shut down the active build and try again.')
await printSubtle(`To reset this error, you may also delete the ${resolve(tempDir)} directory.`)
process.exit(1)
}
let removed = false
const onClose = () => {
// Prevent double-calling
if (removed) return
removed = true
// Remove the temporary directories
removeDirectory(tempDir)
removeDirectory(`${tempDir}.services`)
}
// Always clear the temp directories on exit
onCleanup(onClose)
return {
close: onClose
}
}
export const getDefaultMainLocation = (outDir) => join(outDir, 'main.js')
const getOS = () => process.platform === 'win32' ? 'windows' : (process.platform === 'darwin' ? 'mac' : 'linux')
export const PLATFORM = getOS() // Declared Mobile OR Implicit Desktop Patform
export const isDesktop = (target: TargetType) => validDesktopTargets.includes(target)
export const isMobile = (target: TargetType) => validMobileTargets.includes(target)
export const normalizeTarget = (target: TargetType) => {
const isDesktopTarget = isDesktop(target)
const isMobileTarget = isMobile(target)
return isDesktopTarget ? 'desktop' : isMobileTarget ? 'mobile' : 'web'
}
export const ensureTargetConsistent = async (target: TargetType, allow = []) => {
if (allow.includes(target)) return target
if (!target) target = 'web' // Default to web target
if (target === 'mobile') target = PLATFORM === 'mac' ? 'ios' : 'android' // Auto-detect mobile platform
if (target === 'desktop') target = 'electron' // Auto-detect desktop platform
const _chalk = await chalk
// Provide a custom warning message for tauri
if (target === 'tauri') {
console.error(_chalk.yellow(`Tauri is not yet supported.`))
process.exit(1)
}
if (universalTargetTypes.includes(target)) return target
if (isDesktop(target)) return target
else if (isMobile(target) && (PLATFORM === 'mac' || target === 'mobile' || target === 'android')) return target // Linux and Windows can build for android
console.error(`No commoners command for ${_chalk.bold(target)} on ${_chalk.bold(PLATFORM)}`)
process.exit(1)
}
// Get Configuration File and Path
export const rootDir = dirname(require.resolve(__filename))
export const templateDir = join(rootDir, 'assets')
export const getBuildConfig = (): WritableElectronBuilderConfig => yaml.load(readFileSync(join(templateDir, 'electron', 'electron-builder.yml')).toString())
// const resolveKey = (key) => {
// if (valid.mode.includes(key)) return MODE
// else if (valid.platform.includes(key)) return PLATFORM
// else if (valid.target.includes(key)) return TARGET
// return
// }
// export const resolvePlatformSpecificValue = (o) => {
// if (o && typeof o === 'object') {
// const resolvedKey = Object.keys(o).find(resolveKey)
// if (resolvedKey) return resolvePlatformSpecificValue(o[resolvedKey]) // Return resolved value
// }
// else if (typeof o === 'function') return resolvePlatformSpecificValue(o())
// return o
// }