|
1 | | -import { writeFileSync, existsSync } from 'fs' |
| 1 | +import { writeFileSync, existsSync, mkdirSync } from 'fs' |
| 2 | +import { readdir, stat, copyFile } from 'fs/promises' |
| 3 | +import { join, isAbsolute, dirname } from 'path' |
2 | 4 |
|
3 | 5 | import prompts from 'prompts' |
4 | 6 |
|
5 | 7 | import { log } from '..' |
6 | | -import { Config, configPath, getLatestFF, SupportedProducts } from '../utils' |
| 8 | +import { |
| 9 | + Config, |
| 10 | + configPath, |
| 11 | + getLatestFF, |
| 12 | + projectDir, |
| 13 | + SupportedProducts, |
| 14 | +} from '../utils' |
7 | 15 |
|
8 | | -const sleep = (time: number) => new Promise<void>((r) => setTimeout(r, time)) |
| 16 | +// ============================================================================= |
| 17 | +// User interaction portion |
9 | 18 |
|
10 | 19 | export async function setupProject() { |
11 | | - if (existsSync(configPath)) { |
12 | | - log.warning('There is already a config file. This will overwrite it!') |
13 | | - await sleep(1000) |
14 | | - } |
| 20 | + try { |
| 21 | + if (existsSync(configPath)) { |
| 22 | + log.warning('There is already a config file. This will overwrite it!') |
| 23 | + await sleep(1000) |
| 24 | + } |
15 | 25 |
|
16 | | - // Ask user for assorted information |
17 | | - const { product } = await prompts({ |
18 | | - type: 'select', |
19 | | - name: 'product', |
20 | | - message: 'Select a product to fork', |
21 | | - choices: [ |
22 | | - { title: 'Firefox stable', value: SupportedProducts.Firefox }, |
23 | | - { |
24 | | - title: 'Firefox extended support (older)', |
25 | | - value: SupportedProducts.FirefoxESR, |
26 | | - }, |
| 26 | + // Ask user for assorted information |
| 27 | + const { product } = await prompts({ |
| 28 | + type: 'select', |
| 29 | + name: 'product', |
| 30 | + message: 'Select a product to fork', |
| 31 | + choices: [ |
| 32 | + { title: 'Firefox stable', value: SupportedProducts.Firefox }, |
| 33 | + { |
| 34 | + title: 'Firefox extended support (older)', |
| 35 | + value: SupportedProducts.FirefoxESR, |
| 36 | + }, |
| 37 | + { |
| 38 | + title: 'Firefox extended support (newer)', |
| 39 | + value: SupportedProducts.FirefoxESRNext, |
| 40 | + }, |
| 41 | + { |
| 42 | + title: 'Firefox developer edition (Not recommended)', |
| 43 | + value: SupportedProducts.FirefoxDev, |
| 44 | + }, |
| 45 | + { |
| 46 | + title: 'Firefox beta (Not recommended)', |
| 47 | + value: SupportedProducts.FirefoxBeta, |
| 48 | + }, |
| 49 | + { |
| 50 | + title: 'Firefox Nightly (Not recommended)', |
| 51 | + value: SupportedProducts.FirefoxNightly, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }) |
| 55 | + |
| 56 | + if (typeof product == 'undefined') return |
| 57 | + |
| 58 | + const productVersion = await getLatestFF(product) |
| 59 | + |
| 60 | + const { version, name, appId, vendor } = await prompts([ |
27 | 61 | { |
28 | | - title: 'Firefox extended support (newer)', |
29 | | - value: SupportedProducts.FirefoxESRNext, |
| 62 | + type: 'text', |
| 63 | + name: 'version', |
| 64 | + message: 'Enter the version of this product', |
| 65 | + initial: productVersion, |
30 | 66 | }, |
31 | 67 | { |
32 | | - title: 'Firefox developer edition (Not recommended)', |
33 | | - value: SupportedProducts.FirefoxDev, |
| 68 | + type: 'text', |
| 69 | + name: 'name', |
| 70 | + message: 'Enter a product name', |
| 71 | + initial: 'Example browser', |
34 | 72 | }, |
35 | 73 | { |
36 | | - title: 'Firefox beta (Not recommended)', |
37 | | - value: SupportedProducts.FirefoxBeta, |
| 74 | + type: 'text', |
| 75 | + name: 'vendor', |
| 76 | + message: 'Enter a vendor', |
| 77 | + initial: 'Example company', |
38 | 78 | }, |
39 | 79 | { |
40 | | - title: 'Firefox Nightly (Not recommended)', |
41 | | - value: SupportedProducts.FirefoxNightly, |
| 80 | + type: 'text', |
| 81 | + name: 'appId', |
| 82 | + message: 'Enter an appid', |
| 83 | + initial: 'com.example.browser', |
| 84 | + // Horrible validation to make sure people don't chose something entirely wrong |
| 85 | + validate: (t: string) => t.includes('.'), |
42 | 86 | }, |
43 | | - ], |
44 | | - }) |
45 | | - |
46 | | - if (typeof product == 'undefined') return |
47 | | - |
48 | | - const productVersion = await getLatestFF(product) |
49 | | - |
50 | | - const { version, name, appId, vendor } = await prompts([ |
51 | | - { |
52 | | - type: 'text', |
53 | | - name: 'version', |
54 | | - message: 'Enter the version of this product', |
55 | | - initial: productVersion, |
56 | | - }, |
57 | | - { |
58 | | - type: 'text', |
59 | | - name: 'name', |
60 | | - message: 'Enter a product name', |
61 | | - initial: 'Example browser', |
62 | | - }, |
63 | | - { |
64 | | - type: 'text', |
65 | | - name: 'vendor', |
66 | | - message: 'Enter a vendor', |
67 | | - initial: 'Example company', |
68 | | - }, |
69 | | - { |
70 | | - type: 'text', |
71 | | - name: 'appId', |
72 | | - message: 'Enter an appid', |
73 | | - initial: 'com.example.browser', |
74 | | - // Horrible validation to make sure people don't chose something entirely wrong |
75 | | - validate: (t: string) => t.includes('.'), |
76 | | - }, |
77 | | - ]) |
78 | | - |
79 | | - const config: Config = { |
80 | | - name, |
81 | | - vendor, |
82 | | - appId, |
83 | | - version: { product, version, displayVersion: '1.0.0' }, |
| 87 | + ]) |
| 88 | + |
| 89 | + const config: Config = { |
| 90 | + name, |
| 91 | + vendor, |
| 92 | + appId, |
| 93 | + version: { product, version, displayVersion: '1.0.0' }, |
| 94 | + } |
| 95 | + |
| 96 | + await copyRequired() |
| 97 | + |
| 98 | + writeFileSync(configPath, JSON.stringify(config, null, 2)) |
| 99 | + } catch (e) { |
| 100 | + console.log(e) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// ============================================================================= |
| 105 | +// Filesystem templating |
| 106 | + |
| 107 | +const templateDir = join(__dirname, '../..', 'template') |
| 108 | + |
| 109 | +async function copyRequired() { |
| 110 | + try { |
| 111 | + await Promise.all( |
| 112 | + ( |
| 113 | + await walkDirectory(templateDir) |
| 114 | + ) |
| 115 | + .filter((f) => !f.includes('.optional')) |
| 116 | + .map(async (file) => { |
| 117 | + const out = join(projectDir, file.replace(templateDir, '')) |
| 118 | + if (!existsSync(out)) { |
| 119 | + mkdirSync(dirname(out), { recursive: true }) |
| 120 | + await copyFile(file, out) |
| 121 | + } |
| 122 | + }) |
| 123 | + ) |
| 124 | + } catch (e) { |
| 125 | + console.log(e) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// ============================================================================= |
| 130 | +// Utility functions |
| 131 | + |
| 132 | +const sleep = (time: number) => new Promise<void>((r) => setTimeout(r, time)) |
| 133 | + |
| 134 | +async function walkDirectory(dirName: string): Promise<string[]> { |
| 135 | + const output = [] |
| 136 | + |
| 137 | + if (!isAbsolute(dirName)) { |
| 138 | + log.askForReport() |
| 139 | + log.error('Please provide an absolute input to walkDirectory') |
| 140 | + } |
| 141 | + |
| 142 | + try { |
| 143 | + const directoryContents = await readdir(dirName) |
| 144 | + |
| 145 | + for (const file of directoryContents) { |
| 146 | + const fullPath = join(dirName, file) |
| 147 | + const fStat = await stat(fullPath) |
| 148 | + |
| 149 | + if (fStat.isDirectory()) { |
| 150 | + for (const newFile of await walkDirectory(fullPath)) { |
| 151 | + output.push(newFile) |
| 152 | + } |
| 153 | + } else { |
| 154 | + output.push(fullPath) |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (e) { |
| 158 | + log.askForReport() |
| 159 | + log.error(e) |
84 | 160 | } |
85 | 161 |
|
86 | | - writeFileSync(configPath, JSON.stringify(config, null, 2)) |
| 162 | + return output |
87 | 163 | } |
0 commit comments