Denoでdotfilesを書く
できるんじゃないか?
動機
好きではないが書きやすいTypeScriptとDenoで書けたら楽になりそう 実装
Denoのインストールや必要な環境変数の設定は先にやる
code:index.sh
# install deno
if !(type deno > /dev/null 2>&1); then
sudo apt install unzip
fi
export DENO_INSTALL="$HOME/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
code:bash_env.sh
export DENO_INSTALL="$HOME/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
export EDITOR=vim
export GIT_EDITOR=vim
export CC=clang
export CXX=clang++
export XDG_CONFIG_HOME=$HOME/.config
export PATH="$HOME/.local/bin:$PATH"
export LD_LIBRARY_PATH=$HOME/.local/lib/:$LD_LIBRARY_PATH
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
export PATH=$HOME/.nodebrew/current/bin:$PATH
export PATH=$HOME/.cargo/bin:$PATH
alias ls='exa --icons --color-scale --git'
alias vim='NVIM_LISTEN_ADDRESS=/tmp/nvimsocket nvim'
alias la='ls -Gla'
# bat の設定
if type batcat > /dev/null 2>&1; then
export BAT_THEME=zenburn
alias cat=batcat
fi
# yarnのinstall
if !(type yarn > /dev/null 2>&1); then
echo "yarn isn't installed. Adding the repository of yarn..."
sudo apt update
if type nvm > /dev/null 2>&1; then
echo "nvm is found"
sudo apt install --no-install-recommends yarn
else
sudo apt install yarn
fi
fi
# gitをhubに置き換える
if type hub > /dev/null 2>&1; then
eval "$(hub alias -s)"
fi
# pyenvのpath設定
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
テストコード
code:index.ts
const value1 = Deno.run({
cwd: '/',
code:index.ts
stdout: 'piped',
});
console.log(new TextDecoder().decode(await value1.output()));
const value2 = Deno.run({cmd: 'sudo apt-get install cowsay'.split(/\s/), stderr: 'piped', stdout: 'piped',});
value2.status(),
value2.output(),
value2.stderrOutput()
]);
value2.close();
console.log({
status,
stdout: new TextDecoder().decode(stdout),
stderr: new TextDecoder().decode(stderr),
});
code:test.ts
const {output: cowsayOut} = await exec('which cowsay');
if (!cowsayOut) console.log(await exec('sudo apt-get install cowsay'));
const {output} = await exec('cowsay "Hello, from Shell via Deno"');
↓これだとsudo apt install cowsayの一部が読み取れない
code:test2.ts
async function execute(command: string) {
const process = Deno.run({cmd: command.split(/\s/), stderr: 'piped', stdout: 'piped',});
process.status(),
process.output(),
process.stderrOutput(),
]);
return {
status,
stdout: new TextDecoder().decode(stdout),
stderr: new TextDecoder().decode(stderr),
};
}
環境変数用
code:test3.ts
type Config = {
name: string;
value: string;
reset: boolean;
};
const setEnv = (config: Config[]) => {
config.map(({name, value, reset}) => {})
}