Skip to content

Commit

Permalink
feat: export fs2 module
Browse files Browse the repository at this point in the history
Also, remove all `index.ts` files except root, re-export all
from root, no more deep-imports.
  • Loading branch information
kirillgroshkov committed Aug 13, 2023
1 parent 55831fa commit 92ba4de
Show file tree
Hide file tree
Showing 36 changed files with 148 additions and 110 deletions.
2 changes: 1 addition & 1 deletion scripts/backpressureSimulation.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ yarn tsn backpressureSimulation.script.ts

import { pDelay, _range } from '@naturalcycles/js-lib'
import { Debug, readableFromArray, transformLogProgress, writableForEach, _pipeline } from '../src'
import { runScript } from '../src/script'
import { runScript } from '../src/script/runScript'

Debug.enable('*')

Expand Down
4 changes: 2 additions & 2 deletions scripts/colors.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ yarn tsn colors.script.ts
*/

import { Color, Modifiers } from 'chalk'
import { chalk } from '../src/colors'
import { runScript } from '../src/script'
import { chalk } from '../src/colors/colors'
import { runScript } from '../src/script/runScript'

const s = 'Hello World! 1 2 3 4 5ms'

Expand Down
2 changes: 1 addition & 1 deletion scripts/ndjsonForEach.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ yarn tsn ndjsonForEach.script
*/

import { ndjsonStreamForEach } from '../src'
import { runScript } from '../src/script'
import { runScript } from '../src/script/runScript'
import { tmpDir } from '../src/test/paths.cnst'

runScript(async () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/ndjsonMap.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createWriteStream } from 'node:fs'
import * as fs from 'node:fs'
import { _range } from '@naturalcycles/js-lib'
import { readableFromArray, transformToNDJson, _pipeline } from '../src'
import { runScript } from '../src/script'
import { runScript } from '../src/script/runScript'
import { ndjsonMap } from '../src'
import { tmpDir } from '../src/test/paths.cnst'

Expand Down
2 changes: 1 addition & 1 deletion src/bin/del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as yargs from 'yargs'
import { delSync } from '../fs/del'
import { runScript } from '../script'
import { runScript } from '../script/runScript'

runScript(async () => {
const { _: patterns, ...opt } = yargs.demandCommand(1).options({
Expand Down
4 changes: 2 additions & 2 deletions src/bin/generate-build-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
import * as yargs from 'yargs'
import { appendToBashEnv, appendToGithubEnv, appendToGithubOutput } from '../fs'
import { runScript } from '../script'
import { appendToBashEnv, appendToGithubEnv, appendToGithubOutput } from '../fs/json2env'
import { runScript } from '../script/runScript'
import { generateBuildInfo } from '../util/buildInfo.util'

runScript(async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/json2env.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import * as yargs from 'yargs'
import { json2env } from '../fs'
import { runScript } from '../script'
import { json2env } from '../fs/json2env'
import { runScript } from '../script/runScript'

runScript(() => {
const { argv } = yargs.demandCommand(1).options({
Expand Down
2 changes: 1 addition & 1 deletion src/bin/kpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as yargs from 'yargs'
import { kpySync } from '../fs/kpy'
import { runScript } from '../script'
import { runScript } from '../script/runScript'

runScript(() => {
const {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/secrets-decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import * as yargs from 'yargs'
import { dimGrey } from '../colors'
import { runScript } from '../script'
import { dimGrey } from '../colors/colors'
import { runScript } from '../script/runScript'
import { DecryptCLIOptions, secretsDecrypt } from '../secret/secrets-decrypt.util'

runScript(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/secrets-encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

import * as yargs from 'yargs'
import { dimGrey } from '../colors'
import { runScript } from '../script'
import { dimGrey } from '../colors/colors'
import { runScript } from '../script/runScript'
import { EncryptCLIOptions, secretsEncrypt } from '../secret/secrets-encrypt.util'

runScript(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/secrets-gen-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import * as crypto from 'node:crypto'
import * as yargs from 'yargs'
import { dimGrey } from '../colors'
import { runScript } from '../script'
import { dimGrey } from '../colors/colors'
import { runScript } from '../script/runScript'

runScript(() => {
const { sizeBytes } = yargs.option('sizeBytes', {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/slack-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as yargs from 'yargs'
import { SlackService } from '..'
import { runScript } from '../script'
import { runScript } from '../script/runScript'

runScript(async () => {
const {
Expand Down
3 changes: 1 addition & 2 deletions src/colors/colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { hasColors } from './colors'
import { grey } from '.'
import { hasColors, grey } from './colors'

test('colors', () => {
console.log(grey('sdf'))
Expand Down
35 changes: 35 additions & 0 deletions src/colors/colors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
import * as tty from 'node:tty'
import * as chalk from 'chalk'

export { chalk }

/**
* Based on: https://github.com/sindresorhus/yoctocolors/pull/5
*
* @experimental
*/
export const hasColors = !process.env['NO_COLOR'] && tty.WriteStream.prototype.hasColors()

// The point of re-exporting is:
// 1. Fix typings to allow to pass `number` (very common case)
// 2. Easier/shorter to import, rather than from 'chalk'
// export type ColorFn = (...args: (string | number)[]) => string

export const white = chalk.white
export const dimWhite = chalk.dim.white
export const boldWhite = chalk.bold.white
export const inverseWhite = chalk.inverse.white
export const grey = chalk.grey
export const dimGrey = chalk.dim.grey
export const boldGrey = chalk.bold.grey
export const yellow = chalk.yellow
export const dimYellow = chalk.dim.yellow
export const boldYellow = chalk.bold.yellow
export const inverseYellow = chalk.inverse.yellow
export const green = chalk.green
export const dimGreen = chalk.dim.green
export const boldGreen = chalk.bold.green
export const red = chalk.red
export const dimRed = chalk.dim.red
export const boldRed = chalk.bold.red
export const blue = chalk.blue
export const dimBlue = chalk.dim.blue
export const boldBlue = chalk.bold.blue
export const magenta = chalk.magenta
export const dimMagenta = chalk.dim.magenta
export const boldMagenta = chalk.bold.magenta
export const cyan = chalk.cyan
export const dimCyan = chalk.dim.cyan
export const boldCyan = chalk.bold.cyan
35 changes: 0 additions & 35 deletions src/colors/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/fs/del.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import { pFilter, pMap, _since } from '@naturalcycles/js-lib'
import { dimGrey, yellow } from '../colors'
import { dimGrey, yellow } from '../colors/colors'
import { _pathExists, _pathExistsSync, globby } from '../index'

export interface DelOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/fs/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import { projectDir, scriptsDir, tmpDir } from '../test/paths.cnst'
import { json2env, kpy, kpySync } from './index'
import { json2env, kpy, kpySync } from '../'

beforeEach(() => {
jest.spyOn(fs, 'writeFileSync').mockImplementation()
Expand Down
6 changes: 6 additions & 0 deletions src/fs/fs.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'node:fs'
import { testDir } from '../test/paths.cnst'
import {
_ensureDir,
Expand All @@ -10,6 +11,7 @@ import {
_readFileSync,
_readJson,
_readJsonSync,
fs2,
} from './fs.util'

test('readFile', async () => {
Expand Down Expand Up @@ -43,3 +45,7 @@ test('readFile', async () => {
_ensureFileSync(someFilePath)
await _ensureFile(someFilePath)
})

test('extends native fs', () => {
expect(fs2.createWriteStream).toBe(fs.createWriteStream)
})
83 changes: 53 additions & 30 deletions src/fs/fs.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,48 @@ import * as fsp from 'node:fs/promises'
import * as path from 'node:path'
import { _jsonParse } from '@naturalcycles/js-lib'

/**
* fs2 conveniently groups filesystem functions together.
* Supposed to be almost a drop-in replacement for these things together:
*
* 1. node:fs
* 2. node:fs/promises
* 3. fs-extra
*/
export const fs2 = {
// "Omit" is here to workaround this TS error:
// Exported variable 'fs2' has or is using name 'StreamOptions' from external module "fs" but cannot be named.
...(fs as Omit<typeof fs, 'StreamOptions'>),
readFile: _readFile,
readFileSync: _readFileSync,
readFileAsBuffer: _readFileAsBuffer,
readFileAsBufferSync: _readFileAsBufferSync,
readJson: _readJson,
readJsonSync: _readJsonSync,
writeFile: _writeFile,
writeFileSync: _writeFileSync,
outputJson: _outputJson,
outputJsonSync: _outputJsonSync,
writeJson: _writeJson,
writeJsonSync: _writeJsonSync,
outputFile: _outputFile,
outputFileSync: _outputFileSync,
pathExists: _pathExists,
pathExistsSync: _pathExistsSync,
ensureDir: _ensureDir,
ensureDirSync: _ensureDirSync,
ensureFile: _ensureFile,
ensureFileSync: _ensureFileSync,
removePath: _removePath,
removePathSync: _removePathSync,
emptyDir: _emptyDir,
emptyDirSync: _emptyDirSync,
copyPath: _copyPath,
copyPathSync: _copyPathSync,
movePath: _movePath,
movePathSync: _movePathSync,
}

export interface JsonOptions {
spaces?: number
}
Expand All @@ -30,33 +72,34 @@ export async function _readFile(filePath: string): Promise<string> {
return await fsp.readFile(filePath, 'utf8')
}

export async function _readFileAsBuffer(filePath: string): Promise<Buffer> {
return await fsp.readFile(filePath)
}

/**
* Convenience wrapper that defaults to utf-8 string output.
*/
export function _readFileSync(filePath: string): string {
return fs.readFileSync(filePath, 'utf8')
}

/**
* Convenience wrapper that defaults to utf-8 string output.
*/
export function _readFileAsBufferSync(filePath: string): Buffer {
return fs.readFileSync(filePath)
}

export async function _readJson<T = unknown>(filePath: string): Promise<T> {
const str = await fsp.readFile(filePath, 'utf8')
return _jsonParse(str)
}

/**
* @deprecated use _readJson
*/
export const _readJsonFile = _readJson

export function _readJsonSync<T = unknown>(filePath: string): T {
const str = fs.readFileSync(filePath, 'utf8')
return _jsonParse(str)
}

/**
* @deprecated use _readJsonSync
*/
export const _readJsonFileSync = _readJsonSync

export async function _writeFile(filePath: string, data: string | Buffer): Promise<void> {
await fsp.writeFile(filePath, data)
}
Expand Down Expand Up @@ -88,41 +131,21 @@ export async function _writeJson(filePath: string, data: any, opt?: JsonOptions)
await fsp.writeFile(filePath, str)
}

/**
* @deprecated use _writeJson
*/
export const _writeJsonFile = _writeJson

export function _writeJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
const str = JSON.stringify(data, null, opt?.spaces)
fs.writeFileSync(filePath, str)
}

/**
* @deprecated use _writeJsonSync
*/
export const _writeJsonFileSync = _writeJsonSync

export async function _outputJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
const str = JSON.stringify(data, null, opt?.spaces)
await _outputFile(filePath, str)
}

/**
* @deprecated use _outputJson
*/
export const _outputJsonFile = _outputJson

export function _outputJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
const str = JSON.stringify(data, null, opt?.spaces)
_outputFileSync(filePath, str)
}

/**
* @deprecated use _outputJsonSync
*/
export const _outputJsonFileSync = _outputJsonSync

export async function _pathExists(filePath: string): Promise<boolean> {
try {
await fsp.access(filePath)
Expand Down
3 changes: 0 additions & 3 deletions src/fs/index.ts

This file was deleted.

Loading

0 comments on commit 92ba4de

Please sign in to comment.