Skip to content

Commit

Permalink
Maorba/feature/mapps local repository token (#50)
Browse files Browse the repository at this point in the history
* init. pun intended.

* add "local" flag to init
add gitignore update configuration
use local folder for config if exists

* bump version
remove logs

* lint

* bump version to 1.4.0
  • Loading branch information
maorb-dev authored Oct 29, 2023
1 parent f146e4f commit 6e71285
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mondaycom/apps-cli",
"version": "1.3.5",
"version": "1.4.0",
"description": "A cli tool to manage apps (and monday-code projects) in monday.com",
"author": "monday.com Apps Team",
"type": "module",
Expand Down
13 changes: 13 additions & 0 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Flags } from '@oclif/core';
import { BaseCommand } from 'commands-base/base-command';
import { CONFIG_KEYS } from 'consts/config';
import { CONFIG_NAME, ConfigService } from 'services/config-service';
import { getCurrentWorkingDirectory } from 'services/env-service';
import { createGitignoreAndAppendConfigFileIfNeeded } from 'services/files-service';
import { PromptService } from 'services/prompt-service';
import { InitCommandArguments } from 'types/commands/init';
import logger from 'utils/logger';
Expand All @@ -27,6 +29,12 @@ export default class Init extends BaseCommand {
char: 't',
description: 'monday.com api access token (https://developer.monday.com/api-reference/docs/authentication)',
}),
local: Flags.boolean({
char: 'l',
description: 'create the configuration file locally, in the current project working directory',
default: false,
required: false,
}),
});

static args = {};
Expand All @@ -39,6 +47,11 @@ export default class Init extends BaseCommand {
};

try {
if (flags.local) {
this.config.configDir = getCurrentWorkingDirectory();
createGitignoreAndAppendConfigFileIfNeeded(this.config.configDir);
}

ConfigService.init(args, this.config.configDir, { override: true, setInProcessEnv: true });
logger.info(`'${CONFIG_NAME}' created`);
} catch (error) {
Expand Down
10 changes: 7 additions & 3 deletions src/hooks/init.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Command } from '@oclif/core';

import { ConfigService } from 'services/config-service';
import { getAppsDomain, initCurrentWorkingDirectory } from 'services/env-service';
import { getAppsDomain, getCurrentWorkingDirectory, initCurrentWorkingDirectory } from 'services/env-service';
import { enablePrintCommand } from 'utils/command-printer';
import logger, { enableDebugMode } from 'utils/logger';

export default function init(opts: Command) {
initCurrentWorkingDirectory();

if (ConfigService.checkLocalConfigExists()) {
opts.config.configDir = getCurrentWorkingDirectory();
}

ConfigService.loadConfigToProcessEnv(opts.config.configDir);
if (opts.argv.includes('--verbose')) {
enableDebugMode();
Expand All @@ -15,6 +21,4 @@ export default function init(opts: Command) {
if (opts.argv.includes('--print-command') || opts.argv.includes('--pc')) {
enablePrintCommand();
}

initCurrentWorkingDirectory();
}
11 changes: 11 additions & 0 deletions src/services/config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from '
import { join } from 'node:path';

import { BadConfigError } from 'errors/bad-config-error';
import { getCurrentWorkingDirectory } from 'services/env-service';
import { ConfigData, InitConfigOptions } from 'types/services/config-service';
import logger from 'utils/logger';

Expand All @@ -19,6 +20,14 @@ const checkConfigExists = (directoryPath: string, fileName = CONFIG_NAME) => {
return configExists;
};

const checkLocalConfigExists = (fileName = CONFIG_NAME) => {
const filePath = join(getCurrentWorkingDirectory(), fileName);
const localConfigExists = existsSync(filePath);
if (!configExists) configExists = localConfigExists;

return localConfigExists;
};

const camelToUpperSnakeCase = (str: string) => str.replaceAll(/[A-Z]/g, letter => `_${letter}`).toUpperCase();

const generateConfigKeyInProcessEnv = (configKey: keyof ConfigData) => {
Expand Down Expand Up @@ -57,6 +66,8 @@ const writeConfig = (data: ConfigData, directoryPath: string, fileName = CONFIG_
export const ConfigService = {
checkConfigExists,

checkLocalConfigExists,

getConfigDataByKey(key: keyof ConfigData): string {
const configKeyInProcessEnv = generateConfigKeyInProcessEnv(key);
return process.env[configKeyInProcessEnv] as string;
Expand Down
14 changes: 14 additions & 0 deletions src/services/files-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import archiver from 'archiver';
import glob from 'glob';
import parseGitIgnore from 'parse-gitignore';

import { CONFIG_NAME } from 'services/config-service';

import logger from '../utils/logger.js';

export const readFileData = (filePath: string): Buffer => {
Expand Down Expand Up @@ -46,6 +48,18 @@ export const createTarGzArchive = async (directoryPath: string, fileName = 'code
}
};

export const createGitignoreAndAppendConfigFileIfNeeded = (directoryPath: string, fileName = CONFIG_NAME) => {
const filePath = path.join(directoryPath, '.gitignore');
if (!checkIfFileExists(filePath)) {
fs.writeFileSync(filePath, '', 'utf8');
}

const gitignoreContent = fs.readFileSync(filePath, 'utf8');
if (!gitignoreContent.includes(fileName)) {
fs.appendFileSync(filePath, `\n${fileName}`, 'utf8');
}
};

const getFilesToExcludeForArchive = (directoryPath: string): string[] => {
const DEBUG_TAG = 'ignore_files_for_archive';
const mappsIgnorePath = getIgnorePath(directoryPath, '.mappsignore');
Expand Down
2 changes: 1 addition & 1 deletion src/services/push-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export const buildAssetToDeployTask = async (
try {
if (!ctx.directoryPath) {
const currentDirectoryPath = getCurrentWorkingDirectory();
logger.debug(`Directory path not provided using current directory: ${currentDirectoryPath}`);
logger.debug(`Directory path not provided. using current directory: ${currentDirectoryPath}`);
ctx.directoryPath = currentDirectoryPath;
}

Expand Down

0 comments on commit 6e71285

Please sign in to comment.