Skip to content

Commit

Permalink
feat: use cli-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 23, 2024
1 parent 9601980 commit 9d673e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 157 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@dfinity/identity": "^1.0.1",
"@dfinity/principal": "^1.0.1",
"@junobuild/admin": "^0.0.46",
"@junobuild/cli-tools": "^0.0.2",
"@junobuild/config": "^0.0.2",
"@junobuild/core-peer": "^0.0.11",
"@junobuild/utils": "^0.0.19",
Expand Down
101 changes: 17 additions & 84 deletions src/configs/juno.config.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,29 @@
import {
junoConfigExist as junoConfigExistTools,
junoConfigFile as junoConfigFileTools,
readJunoConfig as readJunoConfigTools,
type ConfigFilename
} from '@junobuild/cli-tools';
import type {JunoConfig, JunoConfigEnv, JunoConfigFnOrObject} from '@junobuild/config';
import {nonNullish} from '@junobuild/utils';
import {existsSync} from 'node:fs';
import {access, readFile, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
import {writeFile} from 'node:fs/promises';
import {
TEMPLATE_INIT_PATH,
TEMPLATE_SATELLITE_CONFIG_FILENAME
} from '../constants/config.constants';
import {JUNO_CONFIG_FILENAME, JUNO_JSON} from '../constants/constants';
import {JUNO_CONFIG_FILENAME} from '../constants/constants';
import {DEPLOY_DEFAULT_SOURCE} from '../constants/deploy.constants';
import type {ConfigType, JunoConfigWithSatelliteId} from '../types/config';
import {readTemplateFile} from '../utils/fs.utils';
import {nodeRequire} from '../utils/node.utils';

const JUNO_CONFIG_FILE: {filename: ConfigFilename} = {filename: JUNO_CONFIG_FILENAME};

export const junoConfigExist = async (): Promise<boolean> => {
try {
const {configPath} = junoConfigFile();
await access(configPath);
return true;
} catch (err: unknown) {
if (err instanceof Error && 'code' in err && (err as NodeJS.ErrnoException).code === 'ENOENT') {
return false;
} else {
throw err;
}
}
return await junoConfigExistTools(JUNO_CONFIG_FILE);
};

export const junoConfigFile = (): {configPath: string; configType: ConfigType} => {
const junoTs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.ts`);

if (existsSync(junoTs)) {
return {
configPath: junoTs,
configType: 'ts'
};
}

const junoJs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.js`);

if (existsSync(junoJs)) {
return {
configPath: junoJs,
configType: 'js'
};
}

const junoMjs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.mjs`);

if (existsSync(junoMjs)) {
return {
configPath: junoMjs,
configType: 'js'
};
}

const junoCjs = join(process.cwd(), `${JUNO_CONFIG_FILENAME}.cjs`);

if (existsSync(junoCjs)) {
return {
configPath: junoCjs,
configType: 'js'
};
}

// Support for original juno.json file
const junoJsonDeprecated = join(process.cwd(), JUNO_JSON);

if (existsSync(junoJsonDeprecated)) {
return {
configPath: junoJsonDeprecated,
configType: 'json'
};
}

return {
configPath: join(process.cwd(), `${JUNO_CONFIG_FILENAME}.json`),
configType: 'json'
};
};
export const junoConfigFile = (): {configPath: string; configType: ConfigType} =>
junoConfigFileTools(JUNO_CONFIG_FILE);

export const writeJunoConfig = async ({
config,
Expand Down Expand Up @@ -117,23 +62,11 @@ export const writeJunoConfig = async ({
};

export const readJunoConfig = async (env: JunoConfigEnv): Promise<JunoConfig> => {
const {configPath, configType} = junoConfigFile();

const config = (userConfig: JunoConfigFnOrObject): JunoConfig =>
typeof userConfig === 'function' ? userConfig(env) : userConfig;

switch (configType) {
case 'ts': {
const {default: userConfig} = nodeRequire<JunoConfigFnOrObject>(configPath);
return config(userConfig);
}
case 'js': {
const {default: userConfig} = await import(configPath);
return config(userConfig as JunoConfigFnOrObject);
}
default: {
const buffer = await readFile(configPath);
return JSON.parse(buffer.toString('utf-8'));
}
}
return await readJunoConfigTools({
...JUNO_CONFIG_FILE,
config
});
};
73 changes: 0 additions & 73 deletions src/utils/node.utils.ts

This file was deleted.

0 comments on commit 9d673e9

Please sign in to comment.