Skip to content

Commit

Permalink
feat(bananass-utils): create bananass-utils package (#39)
Browse files Browse the repository at this point in the history
This pull request includes several changes to the `bananass-utils`
package, focusing on module restructuring, configuration updates, and
the addition of tests. The most important changes include the creation
of a new `package.json` for the package, renaming and refactoring of
utility files, and adding a test file for the `getRootDir` function.

### Module restructuring:
*
[`packages/bananass-utils/src/fs/getRootDir.js`](diffhunk://#diff-2d73861b7a48a161b6c7da8bc708a835f7ed4980c32798208f340991861d2bd6L6-R18):
Renamed from `packages/bananass/src/utils/fs/getRootDir.js` and
refactored to use ES module syntax.
*
[`packages/bananass-utils/src/fs/index.js`](diffhunk://#diff-d309721639755fea30f691711d2b84d859f75dc4f3e0cc4bebdf786c00570d5fR5-R17):
Renamed from `packages/bananass/src/utils/fs/index.js` and updated to
use ES module syntax.
*
[`packages/bananass-utils/src/index.js`](diffhunk://#diff-63cf995490728ee71e1a41cc3d6e933dbb9598f4d8a66dd0a73545c5cea7c0eeR1-R18):
Created as the entry file for the `bananass-utils` package.

### Configuration updates:
*
[`packages/bananass-utils/babel.config.cjs`](diffhunk://#diff-32a42043d854a596d61233b88b1a487f3573598afbe3c55b57cf78ae3e01281cR1-R3):
Added to extend the base Babel configuration.
*
[`packages/bananass-utils/package.json`](diffhunk://#diff-d163ef114bef61820f7f4b4d32b3deb5f15dfc2b5375c030716132c1b4c31c88R1-R48):
Created to define the package metadata and scripts.
*
[`packages/bananass-utils/tsconfig.json`](diffhunk://#diff-a53266bd7f210cfde5d4b24b78800f41fdacbd504fea3296501e89690aacd9a5R1-R10):
Added to configure TypeScript compilation.

### Addition of tests:
*
[`packages/bananass-utils/src/fs/getRootDir.test.js`](diffhunk://#diff-c0482ef0af3b1ae543cd63149daad3275a533bcf07ee593e896d1d88e94504e3R1-R78):
Created to test the `getRootDir` function.

### Code updates:
*
[`packages/bananass/src/commands/bananass-build/webpack.js`](diffhunk://#diff-0ce95c8c9e2a9153c60042f1f9db2f62324e019f7980eb2e8ee8a5001fd6e34bR12-R13):
Updated to import `getRootDir` from `bananass-utils/fs` instead of the
local path.
[[1]](diffhunk://#diff-0ce95c8c9e2a9153c60042f1f9db2f62324e019f7980eb2e8ee8a5001fd6e34bR12-R13)
[[2]](diffhunk://#diff-0ce95c8c9e2a9153c60042f1f9db2f62324e019f7980eb2e8ee8a5001fd6e34bL26)
  • Loading branch information
lumirlumir authored Dec 20, 2024
1 parent 9a485c4 commit 585ee31
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 21 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions packages/bananass-utils/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: '../../babel.config.js',
};
48 changes: 48 additions & 0 deletions packages/bananass-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "bananass-utils",
"version": "0.0.0",
"type": "module",
"description": "Utilities except console for Bananass Framework.🍌",
"exports": {
".": {
"types": "./build/index.d.ts",
"default": "./build/index.js"
},
"./fs": {
"types": "./build/fs/index.d.ts",
"default": "./build/fs/index.js"
},
"./package.json": "./package.json"
},
"files": [
"build",
"LICENSE.md",
"README.md"
],
"keywords": [
"bananass",
"baekjoon",
"framework",
"javascript",
"utils"
],
"author": "루밀LuMir <[email protected]> (https://github.com/lumirlumir)",
"license": "MIT",
"homepage": "https://github.com/lumirlumir/npm-bananass",
"repository": {
"type": "github",
"url": "git+https://github.com/lumirlumir/npm-bananass.git",
"directory": "packages/bananass-utils"
},
"bugs": {
"url": "https://github.com/lumirlumir/npm-bananass/issues"
},
"engines": {
"node": ">=20.18.0"
},
"scripts": {
"prepublishOnly": "npm run build",
"build": "npx babel src -d build && npx tsc && cp ../../LICENSE.md ../../README.md .",
"test": "node --test"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
*/

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const { execSync } = require('node:child_process');
const { join, resolve } = require('node:path');
const { existsSync } = require('node:fs');
import { join, resolve } from 'node:path';
import cp from 'node:child_process';
import fs from 'node:fs';

// TODO: Bug Report
// eslint-disable-next-line import/no-unresolved
const { error } = require('bananass-utils-console/theme');
import { error } from 'bananass-utils-console/theme';

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

/**
Expand All @@ -30,27 +30,29 @@ const { error } = require('bananass-utils-console/theme');
* @throws {Error} If `package.json` cannot be found in either the current
* working directory or the Git root directory, or if Git is not installed or accessible.
*/
module.exports = function getRootDir() {
export default function getRootDir() {
const PACKAGE_JSON = 'package.json';

const path = process.cwd();
if (existsSync(join(path, PACKAGE_JSON))) return path;
if (fs.existsSync(join(path, PACKAGE_JSON))) return path;

let pathFallback;
try {
pathFallback = resolve(execSync('git rev-parse --show-toplevel').toString().trim());
} catch {
pathFallback = resolve(
cp.execSync('git rev-parse --show-toplevel').toString().trim(),
);
} catch ({ message }) {
throw new Error(
error(
'Git command failed. Ensure Git is installed and you are inside a Git repository.',
`Git command failed. Ensure Git is installed and you are inside a Git repository - ${message}`,
),
);
}
if (existsSync(join(pathFallback, PACKAGE_JSON))) return pathFallback;
if (fs.existsSync(join(pathFallback, PACKAGE_JSON))) return pathFallback;

throw new Error(
error(
`Cannot find root directory. Ensure ${PACKAGE_JSON} exists in the project root.\n- path: ${path}\n- pathFallback: ${pathFallback}`,
`Cannot find root directory. Ensure ${PACKAGE_JSON} exists in the project root.\n> path: ${path}\n> pathFallback: ${pathFallback}`,
),
);
};
}
78 changes: 78 additions & 0 deletions packages/bananass-utils/src/fs/getRootDir.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @fileoverview Test for `getRootDir.js`.
*/

/* eslint-disable import/extensions */ // TODO: Remove this line after developing `eslint-config-bananass` package.

// --------------------------------------------------------------------------------
// Import
// --------------------------------------------------------------------------------

import { strictEqual, throws } from 'node:assert';
import { describe, it, mock, afterEach } from 'node:test';
import { resolve } from 'node:path';
import cp from 'node:child_process';
import fs from 'node:fs';

import getRootDir from './getRootDir.js';

// --------------------------------------------------------------------------------
// Test
// --------------------------------------------------------------------------------

afterEach(() => {
mock.reset();
});

describe('getRootDir.js', () => {
it('should return `process.cwd()` when `package.json` exists in current directory', () => {
const MOCK_CWD = resolve('my-folder', 'cwd');
const MOCK_VALID_PATHS = [MOCK_CWD, resolve(MOCK_CWD, 'package.json')];

mock.method(process, 'cwd', () => MOCK_CWD);
mock.method(fs, 'existsSync', path => MOCK_VALID_PATHS.includes(path));

strictEqual(getRootDir(), MOCK_CWD);
});

it('should throw error when git command fails', () => {
const MOCK_CWD = resolve('my-folder', 'cwd');
const MOCK_VALID_PATHS = [MOCK_CWD, resolve(MOCK_CWD, '..', 'package.json')];

mock.method(process, 'cwd', () => MOCK_CWD);
mock.method(fs, 'existsSync', path => MOCK_VALID_PATHS.includes(path));
mock.method(cp, 'execSync', () => {
throw new Error('Throw error');
});

throws(() => getRootDir(), { name: 'Error', message: /Git command failed/ });
});

it('should return git root when `package.json` exists only in git root', () => {
const MOCK_CWD = resolve('my-folder', 'cwd');
const MOCK_GIT_ROOT = resolve(MOCK_CWD, '..');
const MOCK_VALID_PATHS = [
MOCK_CWD,
MOCK_GIT_ROOT,
resolve(MOCK_GIT_ROOT, 'package.json'),
];

mock.method(process, 'cwd', () => MOCK_CWD);
mock.method(fs, 'existsSync', path => MOCK_VALID_PATHS.includes(path));
mock.method(cp, 'execSync', () => MOCK_GIT_ROOT);

strictEqual(getRootDir(), MOCK_GIT_ROOT);
});

it('should throw error when `package.json` not found anywhere', () => {
const MOCK_CWD = resolve('my-folder', 'cwd');
const MOCK_GIT_ROOT = resolve(MOCK_CWD, '..');
const MOCK_VALID_PATHS = [MOCK_CWD, MOCK_GIT_ROOT];

mock.method(process, 'cwd', () => MOCK_CWD);
mock.method(fs, 'existsSync', path => MOCK_VALID_PATHS.includes(path));
mock.method(cp, 'execSync', () => MOCK_GIT_ROOT);

throws(() => getRootDir(), { name: 'Error', message: /Cannot find root directory/ });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* @fileoverview Entry file for the `fs` directory.
*/

/* eslint-disable import/extensions, import/prefer-default-export */ // TODO: Remove this line after developing `eslint-config-bananass` package.

// --------------------------------------------------------------------------------
// Require
// Import
// --------------------------------------------------------------------------------

const getRootDir = require('./getRootDir');
import getRootDir from './getRootDir.js';

// --------------------------------------------------------------------------------
// Exports
// Export
// --------------------------------------------------------------------------------

module.exports = {
getRootDir,
};
export { getRootDir };
18 changes: 18 additions & 0 deletions packages/bananass-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @fileoverview Entry file for the `bananass-utils` package.
* @module bananass-utils
*/

/* eslint-disable import/extensions, import/prefer-default-export */ // TODO: Remove this line after developing `eslint-config-bananass` package.

// --------------------------------------------------------------------------------
// Import
// --------------------------------------------------------------------------------

import fs from './fs/index.js';

// --------------------------------------------------------------------------------
// Export
// --------------------------------------------------------------------------------

export { fs };
10 changes: 10 additions & 0 deletions packages/bananass-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "build"
},
"include": ["src/**/*.js"],
"exclude": ["src/**/*.test.js", "src/**/*.spec.js"]
}
3 changes: 2 additions & 1 deletion packages/bananass/src/commands/bananass-build/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
const { resolve } = require('node:path');
const { rmSync } = require('node:fs');

// eslint-disable-next-line import/no-unresolved
const { getRootDir } = require('bananass-utils/fs');
const {
createLogger,
createSpinner,
Expand All @@ -23,7 +25,6 @@ const {
BAEKJOON_PROBLEM_NUMBER_MIN,
BAEKJOON_PROBLEM_NUMBER_MAX,
} = require('../../constants');
const { getRootDir } = require('../../utils/fs');

// --------------------------------------------------------------------------------
// Exports
Expand Down

0 comments on commit 585ee31

Please sign in to comment.