Skip to content

Commit

Permalink
feat: migrate to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
lenconda committed Jun 16, 2021
1 parent 6a9ec51 commit 2fbd045
Show file tree
Hide file tree
Showing 31 changed files with 464 additions and 166 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ docs
.idea
.vscode
*.d.ts
lib
29 changes: 28 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'alloy',
'alloy/typescript',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
semi: ['error', 'always'],
semi: 'off',
'comma-dangle': ['error', 'always-multiline'],
'switch-colon-spacing': ['error', { 'after': true, 'before': false }],
quotes: ['error', 'single'],
Expand All @@ -24,5 +27,29 @@ module.exports = {
'comma-spacing': ['error', { 'before': false, 'after': true }],
'key-spacing': [2, { 'beforeColon': false, 'afterColon': true, 'mode': 'strict' }],
'no-multiple-empty-lines': ['error', { 'max': 1, 'maxEOF': 1 }],
'@typescript-eslint/semi': ['error', 'always'],
'@typescript-eslint/space-infix-ops': ['error', { 'int32Hint': true }],
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/consistent-type-assertions': 'off',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-parameter-properties': 'off',
'@typescript-eslint/type-annotation-spacing': [
'error',
{
'before': true,
'after': true,
'overrides': {
'colon': {
'before': false,
'after': true,
},
},
},
],
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ temp
.versions
.changelog
package-lock.json
lib
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"private": true,
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^15.12.2",
"@typescript-eslint/eslint-plugin": "^4.27.0",
"@typescript-eslint/parser": "^4.27.0",
"babel-eslint": "^10.1.0",
"eslint": "^7.28.0",
"eslint-config-alloy": "^4.1.0",
"jest": "^27.0.4",
"lerna": "^4.0.0",
"typescript": "^4.3.2"
}
Expand Down
11 changes: 11 additions & 0 deletions packages/@dollie/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@dollie/cli`

> TODO: description
## Usage

```
const cli = require('@dollie/cli');
// TODO: DEMONSTRATE API
```
29 changes: 29 additions & 0 deletions packages/@dollie/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@dollie/cli",
"version": "3.0.0",
"description": "Dollie CLI",
"author": "lenconda <[email protected]>",
"homepage": "https://github.com/lenconda/dollie#readme",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/lenconda/dollie.git"
},
"bin": {
"dollie": "bin/dollie.js"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/lenconda/dollie/issues"
},
"dependencies": {
"commander": "^7.2.0"
},
"devDependencies": {
"@types/lodash": "^4.14.170",
"@types/node": "^15.12.2",
"typescript": "^4.3.2"
}
}
1 change: 1 addition & 0 deletions packages/@dollie/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log();
9 changes: 9 additions & 0 deletions packages/@dollie/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
"tsBuildInfoFile": "./lib/.tsbuildinfo"
}
}
6 changes: 0 additions & 6 deletions packages/@dollie/core/lib/index.js

This file was deleted.

5 changes: 4 additions & 1 deletion packages/@dollie/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"typings": "lib/index.d.ts",
"bugs": {
"url": "https://github.com/lenconda/dollie/issues"
},
Expand All @@ -27,6 +28,8 @@
},
"devDependencies": {
"@types/diff": "^5.0.0",
"@types/lodash": "^4.14.170"
"@types/lodash": "^4.14.170",
"@types/node": "^15.12.2",
"typescript": "^4.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { diffLines } = require('diff');
const _ = require('lodash');
import { diffLines } from 'diff';
import _ from 'lodash';
import { DiffChange, PatchTable } from './interfaces';

function diff(originalContent, currentContent) {
const diff = (originalContent: string, currentContent: string): DiffChange[] => {
const changes = diffLines(originalContent, currentContent || originalContent);
const result = [];
const result: DiffChange[] = [];
let lineNumber = 0;

const splitChanges = changes.reduce((result, currentItem) => {
Expand All @@ -26,7 +27,7 @@ function diff(originalContent, currentContent) {
return result;
};

function merge(originalChanges, diffList) {
const merge = (originalChanges: DiffChange[], diffList: DiffChange[][]): DiffChange[] => {
if (!originalChanges) {
return [];
}
Expand All @@ -36,7 +37,7 @@ function merge(originalChanges, diffList) {
}

const originalDiff = Array.from(originalChanges);
const patchTable = {};
const patchTable: PatchTable = {};

for (const currentDiff of diffList) {
for (const change of currentDiff) {
Expand Down Expand Up @@ -74,7 +75,7 @@ function merge(originalChanges, diffList) {
}
}

const blocks = [];
const blocks: Array<Array<DiffChange>> = [];
const patches = Object.keys(patchTable).map(
(patchIndex) => patchTable[patchIndex],
);
Expand Down Expand Up @@ -103,7 +104,7 @@ function merge(originalChanges, diffList) {
}, []);
};

module.exports = {
export {
diff,
merge,
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const VIRTUAL_VOLUME_DESTINATION_PATHNAME = '/template';

module.exports = {
export {
VIRTUAL_VOLUME_DESTINATION_PATHNAME,
};
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
const _ = require('lodash');
import _ from 'lodash';

class InvalidInputError extends Error {
constructor(reason) {
public constructor(reason) {
super(`Invalid input item${_.isString(reason) ? `: ${reason}` : ''}`);
}
}

class ContextError extends Error {
constructor(reason) {
public constructor(reason) {
super(`Invalid context${_.isString(reason) ? `: ${reason}` : ''}`);
}
}

class HTTPNotFoundError extends Error {
constructor() {
public constructor() {
super('Template resource not found');
}
}

class HTTPTimeoutError extends Error {
constructor() {
public constructor() {
super('Download template resource timed out');
}
}

module.exports = {
class DollieError extends Error {
public code: string;

public constructor(message: string) {
super(message);
}
}

export {
InvalidInputError,
ContextError,
HTTPNotFoundError,
HTTPTimeoutError,
DollieError,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const _ = require('lodash');
import { DollieConfig } from './interfaces';
import _ from 'lodash';

const {
InvalidInputError,
ContextError,
Expand All @@ -7,7 +9,7 @@ const { githubOrigin, gitlabOrigin } = require('@dollie/origins');
const { Volume } = require('memfs');
const { loadTemplate } = require('./loader');

function Generator(name, config = {}) {
function Generator(name, config: DollieConfig = {}) {
this.templateName = '';
this.templateOrigin = '';

Expand Down Expand Up @@ -66,6 +68,15 @@ function Generator(name, config = {}) {

return duration;
};

this.getScaffoldConfig = async function() {
const { getScaffoldConfig } = config;
if (!_.isFunction(getScaffoldConfig)) {
return {};
}
const dollieConfigFileContent = virtualVolume.readFileSync();
return await getScaffoldConfig();
};
}

module.exports = Generator;
1 change: 1 addition & 0 deletions packages/@dollie/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

36 changes: 36 additions & 0 deletions packages/@dollie/core/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
DollieOrigin,
} from '@dollie/origins';
import { Change } from 'diff';
import { Volume } from 'memfs';
import fs from 'fs';
import { Options as GotOptions } from 'got/dist/source';

export interface DiffChange extends Change {
conflicted?: boolean;
conflictGroup?: 'former' | 'current';
lineNumber: number;
}

export interface LoaderOptions {
httpProxyUrl?: string;
httpProxyAuth?: string;
maximumRetryCount?: number;
}

export type LoaderConfig = LoaderOptions & GotOptions;

export interface DollieConfig {
origins?: DollieOrigin[];
loader?: LoaderConfig;
getScaffoldConfig?: () => void;
}

export interface PatchTableItem {
changes: Array<DiffChange>;
modifyLength: number;
}
export type PatchTable = Record<string, PatchTableItem>;

export type MemFS = typeof Volume.prototype;
export type FileSystem = MemFS | typeof fs;
Loading

0 comments on commit 2fbd045

Please sign in to comment.