Last active
January 27, 2024 21:35
-
-
Save damif94/23ff24854341dfcf4757bae6833882d2 to your computer and use it in GitHub Desktop.
install.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zx | |
const latestNixModulesUrl = 'https://draftea-cdn-prod.s3.us-east-2.amazonaws.com/xb/nix/nix-modules-latest.zip' | |
const homeManagerPath = `${process.env.HOME}/.config/home-manager` | |
async function getSystemArch() { | |
let arch = await $`uname -m` | |
switch (arch.stdout.trim()) { | |
case 'x86_64': | |
return 'aarch64' | |
default: | |
echo(`Can't translate arch ${arch} to a valid nix value`) | |
process.exit(1) | |
} | |
} | |
async function getSystemOs() { | |
let os = await $`uname -s` | |
switch (os.stdout.trim()) { | |
case 'Darwin': | |
return 'darwin' | |
default: | |
echo(`Can't translate system os ${os} to a valid nix value`) | |
process.exit(1) | |
} | |
} | |
async function getUserName() { | |
let userName = await $`whoami` | |
return userName.stdout.trim() | |
} | |
async function getInstallationInfo() { | |
const sysUserName = await getUserName() | |
const sysArch = await getSystemArch() | |
const sysOs = await getSystemOs() | |
echo(``) | |
let gitUserName = await question(`What is your full name? [Draftea ${sysUserName}]: `) | |
if (gitUserName == ``) { | |
gitUserName = `Draftea ${sysUserName}` | |
} | |
let gitUser = await question(`What is your Github user name? [${sysUserName}]: `) | |
if (gitUser == ``) { | |
gitUser = sysUserName | |
} | |
let gitEmail = await question(`What is your Github email? [${sysUserName}@draftea.com]: `) | |
if (gitEmail == ``) { | |
gitEmail = `${sysUserName}@draftea.com` | |
} | |
return { | |
sysUserName, | |
sysArch, | |
sysOs, | |
gitUserName, | |
gitUser, | |
gitEmail | |
} | |
} | |
function printInstallationInfo(info) { | |
echo(``) | |
echo(`${chalk.cyan('This is your information:')}`) | |
echo(``) | |
echo(`System OS: ${chalk.green(info.sysOs)}`) | |
echo(`System Arch: ${chalk.green(info.sysArch)}`) | |
echo(`System User: ${chalk.green(info.sysUserName)}`) | |
echo(`System Home: ${chalk.green(process.env.HOME)}`) | |
echo(`Git full name: ${chalk.green(info.gitUserName)}`) | |
echo(`Git user: ${chalk.green(info.gitUser)}`) | |
echo(`Git email: ${chalk.green(info.gitEmail)}`) | |
echo(``) | |
} | |
async function downloadZip() { | |
await spinner( | |
'Downloading modules zip file...', | |
() => $`curl -LJO ${latestNixModulesUrl}`, | |
) | |
} | |
async function createHomeManagerPath() { | |
if (fs.existsSync(homeManagerPath)){ | |
await spinner( | |
`Creating backup for old home-manager folder to ${homeManagerPath}-backup...`, | |
() => $`mv ${homeManagerPath} ${homeManagerPath}-backup` | |
) | |
} | |
await spinner( | |
'Creating home-manager folder...', | |
() => $`mkdir -p ${homeManagerPath}` | |
) | |
} | |
async function extractZip() { | |
await spinner( | |
`Extracting modules zip to ${homeManagerPath}...`, | |
() => $`unzip nix-modules-latest.zip -d ${homeManagerPath}` | |
) | |
await spinner( | |
`Removing module zip file...`, | |
() => $`rm nix-modules-latest.zip` | |
) | |
} | |
async function printIsInfoOK() { | |
const isOk = await question(`It's OK the collected information? (y/n) [y]: `) | |
echo(``) | |
if (isOk.toLowerCase() === `n`) { | |
echo(`${chalk.yellow('Installation process aborted...')}`) | |
} | |
} | |
function getReplacers(info) { | |
return { | |
'::sys-user-name::': info.sysUserName, | |
'::sys-arch::': info.sysArch, | |
'::sys-os::': info.sysOs, | |
'::git-user-name::': info.gitUserName, | |
'::git-user::': info.gitUser, | |
'::git-email::': info.gitEmail, | |
'::home-dir::': process.env.HOME, | |
'::home-manager-path::': homeManagerPath, | |
} | |
} | |
function getFilesToReplace() { | |
return [ | |
`${homeManagerPath}/modules/terminal/zsh.nix`, | |
`${homeManagerPath}/draftea/modules/git.nix`, | |
`${homeManagerPath}/user/modules/git.nix`, | |
`${homeManagerPath}/flake.nix`, | |
`${homeManagerPath}/home.nix`, | |
] | |
} | |
async function replaceInFile(filePath, replaceMap) { | |
// Read the content of the file | |
fs.readFile(filePath, 'utf8', (err, data) => { | |
if (err) { | |
console.error(`Error reading file ${filePath}: ${err}`); | |
return; | |
} | |
// Perform replacements based on the provided map | |
for (const [searchValue, replaceValue] of Object.entries(replaceMap)) { | |
const regex = new RegExp(searchValue, 'g'); | |
data = data.replace(regex, replaceValue); | |
} | |
// Write the modified content back to the file | |
fs.writeFile(filePath, data, 'utf8', (err) => { | |
if (err) { | |
console.error(`Error writing to file ${filePath}: ${err}`); | |
} else { | |
console.log(`Replacements in ${filePath} complete.`); | |
} | |
}); | |
}); | |
} | |
async function renamingUserModules(userName) { | |
await spinner( | |
`Creating specific user modules folder...`, | |
() => $`mv ${homeManagerPath}/user ${homeManagerPath}/${userName}` | |
) | |
} | |
function printInstallFinishSteps() { | |
echo(``) | |
echo(`${chalk.cyan('Installation finished...')}`) | |
echo(``) | |
echo(`To finish your environment configuration set the AWS credentials on file`) | |
echo(`${homeManagerPath}/dotfiles/aws/draftea/credentials.`) | |
echo(``) | |
echo(` vi ${homeManagerPath}/dotfiles/aws/draftea/credentials`) | |
echo(``) | |
echo(`Then you can execute home manager switch command to load your profile again.`) | |
echo(``) | |
echo(` home-manager switch -b backup --impure --flake ${homeManagerPath}#draftea`) | |
echo(``) | |
} | |
let arch = await $`uname -m` | |
console.log(arch.stdout.trim()) | |
echo(`Collecting information...`) | |
const installInfo = await getInstallationInfo() | |
printInstallationInfo(installInfo) | |
await printIsInfoOK() | |
await createHomeManagerPath() | |
await downloadZip() | |
await extractZip() | |
let replacers = getReplacers(installInfo) | |
let replacePromises = getFilesToReplace().map((file) => { | |
return replaceInFile(file, replacers) | |
}) | |
await Promise.all(replacePromises) | |
await renamingUserModules(installInfo.sysUserName) | |
printInstallFinishSteps() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment