-
Notifications
You must be signed in to change notification settings - Fork 41
/
produce.ts
236 lines (196 loc) · 7.8 KB
/
produce.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* ./src/commands/produce.ts
* penguins-eggs v.10.0.0 / ecmascript 2020
* author: Piero Proietti
* email: piero.proietti@gmail.com
* license: MIT
*/
import { Command, Flags } from '@oclif/core'
import chalk from 'chalk'
import fs, { link } from 'node:fs'
import path from 'node:path'
import Compressors from '../classes/compressors.js'
import Ovary from '../classes/ovary.js'
import Utils from '../classes/utils.js'
import { IAddons, IExcludes } from '../interfaces/index.js'
import Config from './config.js'
import { Static } from 'ink'
// _dirname
const __dirname = path.dirname(new URL(import.meta.url).pathname)
export default class Produce extends Command {
static description = 'produce a live image from your system whithout your data'
static examples = [
'sudo eggs produce # fast compression',
'sudo eggs produce --max # max compression',
'sudo eggs produce --pendrive # compression optomized pendrive',
'sudo eggs produce --clone # clone',
'sudo eggs produce --cryptedclone # crypted clone',
'sudo eggs produce --basename=colibri',
'sudo eggs produce --theme lastos',
'sudo eggs produce --excludes static # you can customize it',
'sudo eggs produce --excludes homes # exclude /home/*',
'sudo eggs produce --excludes home # exclude ~/*',
]
static flags = {
addons: Flags.string({ description: 'addons to be used: adapt, ichoice, pve, rsupport', multiple: true }),
basename: Flags.string({ description: 'basename' }),
clone: Flags.boolean({ char: 'c', description: 'clone' }),
cryptedclone: Flags.boolean({ char: 'C', description: 'crypted clone' }),
excludes: Flags.string({ description: 'use: static, homes, home', multiple: true }),
help: Flags.help({ char: 'h' }),
links: Flags.string({ description: 'desktop links', multiple: true }),
max: Flags.boolean({ char: 'm', description: 'max compression: xz -Xbcj ...' }),
noicon: Flags.boolean({ char: 'N', description: 'no icon eggs on desktop' }),
nointeractive: Flags.boolean({ char: 'n', description: 'no user interaction' }),
pendrive: Flags.boolean({ char: 'p', description: 'optimized for pendrive: zstd -b 1M -Xcompression-level 15'}),
prefix: Flags.string({ char: 'P', description: 'prefix' }),
release: Flags.boolean({ description: 'release: remove penguins-eggs, calamares and dependencies after installation' }),
script: Flags.boolean({ char: 's', description: 'script mode. Generate scripts to manage iso build' }),
standard: Flags.boolean({ char: 'f', description: 'standard compression: xz -b 1M' }),
theme: Flags.string({ description: 'theme for livecd, calamares branding and partitions' }),
unsecure: Flags.boolean({ char: 'u', description: '/root contents are included on live' }),
verbose: Flags.boolean({ char: 'v', description: 'verbose' }),
yolk: Flags.boolean({ char: 'y', description: 'force yolk renew' }),
}
async run(): Promise<void> {
Utils.titles(this.id + ' ' + this.argv)
const { flags } = await this.parse(Produce)
let pendrive = flags.pendrive !== undefined ? Number(flags.pendrive) : null;
if (Utils.isRoot()) {
/**
* ADDONS dei vendors
* Fino a 3
*/
const addons = []
if (flags.addons) {
const { addons } = flags // array
for (let addon of addons) {
// se non viene specificato il vendor il default è eggs
if (!addon.includes('//')) {
addon = 'eggs/' + addon
}
const dirAddon = path.resolve(__dirname, `../../addons/${addon}`)
if (!fs.existsSync(dirAddon)) {
console.log(dirAddon)
Utils.warning('addon: ' + chalk.white(addon) + ' not found, terminate!')
process.exit()
}
const vendorAddon = addon.slice(0, Math.max(0, addon.search('/')))
const nameAddon = addon.substring(addon.search('/') + 1, addon.length)
if (nameAddon === 'theme') {
flags.theme = vendorAddon
}
}
}
// links check
const myLinks: string[] = []
if (flags.links) {
const { links } = flags
for (const link_ of links) {
if (fs.existsSync(`/usr/share/applications/${link_}.desktop`)) {
myLinks.push(link_)
} else {
Utils.warning('desktop link: ' + chalk.white('/usr/share/applications/' + link_ + '.desktop') + ' not found!')
}
}
}
/**
* composizione dei flag
*/
// excludes
const excludes = {} as IExcludes
excludes.usr = true
excludes.var = true
excludes.static = false
excludes.homes = false
excludes.home = false
if (flags.excludes) {
if (flags.excludes.includes('static')) {
excludes.static = true
}
if (flags.excludes.includes('homes')) {
excludes.homes = true
}
if (flags.excludes.includes('home')) {
excludes.home = true
}
}
let prefix = ''
if (flags.prefix !== undefined) {
prefix = flags.prefix
}
let basename = '' // se vuoto viene definito da loadsetting (default nome dell'host)
if (flags.basename !== undefined) {
basename = flags.basename
}
const compressors = new Compressors()
await compressors.populate()
let compression = compressors.fast()
if (flags.max) {
compression = compressors.max()
} else if (flags.pendrive){
compression = compressors.pendrive('15')
} else if (flags.standard) {
compression = compressors.standard()
}
const { release } = flags
const { cryptedclone } = flags
const { clone } = flags
const { verbose } = flags
const scriptOnly = flags.script
const yolkRenew = flags.yolk
const { nointeractive } = flags
const { noicon } = flags
// if clone or cryptedclone unsecure = true
const unsecure = flags.unsecure || clone || cryptedclone
/**
* theme: if not defined will use eggs
*/
let theme = 'eggs'
if (flags.theme !== undefined) {
theme = flags.theme
if (theme.includes('/')) {
if (theme.endsWith('/')) {
theme = theme.slice(0, Math.max(0, theme.length - 1))
}
} else {
const wpath = `/home/${await Utils.getPrimaryUser()}/.wardrobe/vendors/`
theme = wpath + flags.theme
}
theme = path.resolve(theme)
if (!fs.existsSync(theme + '/theme')) {
console.log('Cannot find theme: ' + theme)
process.exit()
}
}
const i = await Config.thatWeNeed(nointeractive, verbose, cryptedclone)
if ((i.needApt || i.configurationInstall || i.configurationRefresh || i.distroTemplate) && (await Utils.customConfirm('Select yes to continue...'))) {
await Config.install(i, nointeractive, verbose)
}
const myAddons = {} as IAddons
if (flags.addons != undefined) {
if (flags.addons.includes('adapt')) {
myAddons.adapt = true
}
if (flags.addons.includes('ichoice')) {
myAddons.ichoice = true
}
if (flags.addons.includes('pve')) {
myAddons.pve = true
}
if (flags.addons.includes('rsupport')) {
myAddons.rsupport = true
}
}
Utils.titles(this.id + ' ' + this.argv)
const ovary = new Ovary()
Utils.warning('Produce an egg...')
if (await ovary.fertilization(prefix, basename, theme, compression, !nointeractive)) {
await ovary.produce(clone, cryptedclone, scriptOnly, yolkRenew, release, myAddons, myLinks, excludes, nointeractive, noicon, unsecure, verbose)
ovary.finished(scriptOnly)
}
} else {
Utils.useRoot(this.id)
}
}
}