๐ค Chapter 12: ํ์ ์คํฌ๋ฆฝํธ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ค์ต
๐ฆ ๋น ๋ฐ์ดํฐ ๋ฐฐ์น ํ๋ก๊ทธ๋จ ๋ง๋ค๊ธฐโ
- ์ค์ต์ ๋ํ ๋ด์ฉ์ด๋ฏ๋ก ์์ธํ ๋ด์ฉ์ ์ฑ ์ ์ฐธ๊ณ (P.340 ~ P.365)
๐ ๋ ธ๋์ ์ด์์ค์์ ํ๋ก๊ทธ๋จ ๋ช ๋ น ์ค ์ธ์ ์ฝ๊ธฐโ
export type FileNameAndNumber = [string, number];
export const getFileNameAndNumber = (
defaultFilename: string,
defaultNumberOfFakeData: number,
): FileNameAndNumber => {
const [bin, node, filename, numberOfFakeData] = process.argv;
return [
filename || defaultFilename,
numberOfFakeData ? parseInt(numberOfFakeData, 10) : defaultNumberOfFakeData,
];
};
๐ ํ์ผ ์ฒ๋ฆฌ ๋น๋๊ธฐ ํจ์๋ฅผ ํ๋ก๋ฏธ์ค๋ก ๊ตฌํํ๊ฐโ
๐ fs.access API๋ก ๋๋ ํฐ๋ฆฌ๋ ํ์ผ ํ์ธํ๊ธฐโ
- ์ฝ๋๋ฅผ ์์ฑํ๋ค ๋ณด๋ฉด ํ์ผ์ด๋ ๋๋ ํฐ๋ฆฌ๊ฐ ํ์ฌ ์๋์ง ์๋์ง๋ฅผ ํ์ธํด์ผ ํ ๋๊ฐ ์๊ธด๋ค.
import * as fs from 'fs';
export const fileExist = (
filepath: string,
): Promise<boolean> => new Promise((resolve) => fs.access(filepath, (error) => resolve(!error)));
fileExists-test.ts
import { fileExist } from '../fileApi/fileExists';
const exists = async (filepath) => {
const result = await fileExist(filepath);
console.log(`${filepath} ${result ? 'exists' : 'not exists'}`);
};
exists('./package.json');
exists('./package');
๐ mkdirp ํจํค์ง๋ก ๋๋ ํฐ๋ฆฌ ์์ฑ ํจ์ ๋ง๋ค๊ธฐโ
import mkdirp from 'mkdirp';
import { fileExist } from './fileExists';
export const mkdir = (dirname: string): Promise<string> => new Promise(async (resolve, reject) => {
const alreadyExists = await fileExist(dirname);
if (alreadyExists) {
resolve(dirname);
return;
}
mkdirp(dirname)
.then(() => resolve(dirname))
.catch((error) => reject(error));
});
๐ rimraf ํจํค์ง๋ก ๋๋ ํฐ๋ฆฌ ์ญ์ ํจ์ ๋ง๋ค๊ธฐโ
import rimraf from 'rimraf';
import { fileExist } from './fileExists';
export const rmdir = (dirname: string): Promise<string> => new Promise(async (resolve, reject) => {
const alreadyExists = await fileExist(dirname);
if (!alreadyExists) {
resolve(dirname);
return;
}
rimraf(dirname, (error) => (error ? reject(error) : resolve(dirname)));
});
๐ fs.writeFile API๋ก ํ์ผ ์์ฑํ๊ธฐโ
- ๋ ธ๋์ ์ด์์ค ํ๊ฒฝ์์ ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฑฐ๋ ์ธ ๋๋ ๋๋ถ๋ถ ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ ๋์์ผ๋ก ํ๋ค.
- ์ด๋ ํ ์คํธ ๋ฐ์ดํฐ๋ ์ ๋์ฝ๋๋ก ์ฒ๋ฆฌํด์ผ ํ๋ค.
import * as fs from 'fs';
export const writeFile = (
filename: string, data: any,
): Promise<any> => new Promise((resolve, reject) => {
fs.writeFile(filename, data, 'utf8', (error: Error) => {
if (error) {
reject(error);
return;
}
resolve(data);
});
});
writeFile-test.ts
import { writeFile } from '../fileApi/writeFile';
import { mkdir } from '../fileApi/mkdir';
const writeTest = async (filename: string, data: any) => {
const result = await writeFile(filename, data);
console.log(`write ${result} to ${filename}`);
};
mkdir('./data')
.then(() => writeTest('./data/hello.txt', 'hello world'))
.then(() => writeTest('./data/test.json', JSON.stringify({ name: 'Jack', age: 32 }, null, 2)))
.catch((e: Error) => console.log(e.message))
๐ fs.readFile API๋ก ํ์ผ ๋ด์ฉ ์ฝ๊ธฐโ
import * as fs from 'fs';
export const readFile = (filename: string): Promise<any> => new Promise<any>((resolve, reject) => {
fs.readFile(filename, 'utf8', (error: Error, data: any) => {
if (error) {
reject(error);
return;
}
resolve(data);
});
});
readFile-test.ts
import { readFile } from '../fileApi/readFile';
const readTest = async (filename: string) => {
const result = await readFile(filename);
console.log(`read ${result} from ${filename} file.`);
};
readTest('./data/hello.txt')
.then(() => readTest('./data/test.json'))
.catch((e: Error) => console.log(e.message));
๐ fs.appendFile API๋ก ํ์ผ์ ๋ด์ฉ ์ถ๊ฐํ๊ธฐโ
import * as fs from 'fs';
export const appendFile = (
filename: string, data: any,
): Promise<any> => new Promise((resolve, reject) => {
fs.appendFile(filename, data, 'utf8', (error: Error) => {
if (error) {
reject(error);
return;
}
resolve(data);
});
});
appendFile-test.ts
import { appendFile } from '../fileApi/appendFile';
import { mkdir } from '../fileApi/mkdir';
const appendTest = async (filename: string, data: any) => {
const result = await appendFile(filename, data);
console.log(`append ${result} to ${filename}`);
};
mkdir('./data')
.then(() => appendTest('./data/hello.txt', 'Hi there!'))
.catch((e: Error) => console.log(e.message));
๐ fs.unlink API๋ก ํ์ผ ์ญ์ ํ๊ธฐโ
import * as fs from 'fs';
import { fileExists } from './fileExists';
export const deleteFile = (
filename: string,
): Promise<string> => new Promise<any>(async (resolve, reject) => {
const alreadyExists = await fileExists(filename);
if (!alreadyExists) {
resolve(filename);
return;
}
fs.unlink(filename, (error) => (error ? reject(error) : resolve(filename)));
});
deleteFile-test.ts
import { deleteFile } from '../fileApi/deleteFile';
import { rmdir } from '../fileApi/rmdir';
const deleteTest = async (filename: string) => {
const result = await deleteFile(filename);
console.log(`delete ${result} file.`);
};
Promise.all([deleteTest('./data/hello.txt'), deleteTest('./data/test.json')])
.then(() => rmdir('./data'))
.then((dirname) => console.log(`delete ${dirname} dir`))
.catch((e: Error) => console.log(e.message));
๐ src/fileApi/index.ts ํ์ผ ๋ง๋ค๊ธฐโ
import { fileExists } from './fileExists';
import { mkdir } from './mkdir';
import { rmdir } from './rmdir';
import { writeFile } from './writeFile';
import { readFile } from './readFile';
import { appendFile } from './appendFile';
import { deleteFile } from './deleteFile';
export {
fileExists, mkdir, rmdir, writeFile, readFile, appendFile, deleteFile,
};
๐ ๊ทธ๋ด๋ฏํ ๊ฐ์ง ๋ฐ์ดํฐ ๋ง๋ค๊ธฐโ
export interface IFake {
name: string;
email: string;
sentence: string;
profession: string;
birthday: Date;
}
makeFakeData.ts
import { Chance } from 'chance';
import { IFake } from './IFake';
const c = new Chance();
export const makeFakeData = (): IFake => ({
name: c.name(),
email: c.email(),
profession: c.profession(),
birthday: c.birthday(),
sentence: c.sentence(),
});
export { IFake };
makeFakeData-test.ts
import { makeFakeData, IFake } from '../fake/makeFakeData';
const fakeData: IFake = makeFakeData();
console.log(fakeData);
๐ Object.keys์ Object.values ํจ์ ์ฌ์ฉํ๊ธฐโ
import { IFake, makeFakeData } from '../fake/makeFakeData';
const data: IFake = makeFakeData();
const keys = Object.keys(data);
console.log('keys: ', keys);
const values = Object.values(data);
console.log('values: ', values);