Skip to content

Commit

Permalink
feat: first blood, should just work (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Apr 26, 2021
1 parent d048554 commit e7446f9
Show file tree
Hide file tree
Showing 18 changed files with 10,738 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
15 changes: 15 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://unpkg.com/@changesets/config@1.6.0/schema.json",
"changelog": [
"@changesets/changelog-github",
{
"repo": "rx-ts/synckit"
}
],
"commit": false,
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/friendly-tables-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": minor
---

feat: first blood, should just work
3 changes: 3 additions & 0 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"node": "14"
}
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [JounQin]
open_collective: rxts
64 changes: 64 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CI

on:
- push
- pull_request

jobs:
ci:
name: Lint and Test with Node.js ${{ matrix.node }}
strategy:
matrix:
node:
- 10
- 12
- 14
- 16
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@master
with:
node-version: ${{ matrix.node }}

- name: Link Yarn global binaries into PATH
run: echo "$(yarn global bin)" >> $GITHUB_PATH

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install Dependencies
run: yarn --frozen-lockfile

- name: Build, Lint and test
run: |
yarn lint
yarn test
yarn build
env:
EFF_NO_LINK_RULES: true
PARSER_NO_WATCH: true

- name: Codecov
if: matrix.node == 14 # only report coverage once per commit
run: |
yarn global add codecov codacy-coverage
codecov
cat ./coverage/lcov.info | codacy-coverage -u rx-ts -n eslint
env:
CI: 'true'
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODACY_ACCOUNT_TOKEN: ${{ secrets.CODACY_ACCOUNT_TOKEN }}
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
47 changes: 47 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Release

on:
push:
branches:
- main

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Node.js 14.x
uses: actions/setup-node@master
with:
node-version: 14.x

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install Dependencies
run: yarn --frozen-lockfile --ignore-scripts

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@master
with:
# This expects you to have a script called release which does a build for your packages and calls changeset publish
publish: yarn release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
lib
node_modules
1 change: 1 addition & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/lint-staged')
1 change: 1 addition & 0 deletions .simple-git-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@1stg/simple-git-hooks')
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# synckit
Perform async work synchronously in Node.js using a separate process

Perform async work synchronously in Node.js using a separate process
97 changes: 97 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "synckit",
"version": "0.0.0",
"description": "Perform async work synchronously in Node.js using a separate process with first-class TypeScript support",
"repository": "git+https://github.com/rx-ts/synckit.git",
"author": "JounQin <admin@1stg.me>",
"license": "MIT",
"engines": {
"node": ">=4.0"
},
"main": "lib",
"module": "lib/esm",
"es2015": "lib/es2015",
"fesm5": "lib/esm",
"types": "lib",
"files": [
"lib",
"!*.tsbuildinfo"
],
"keywords": [
"deasync",
"sync",
"sync-exec",
"sync-rpc",
"sync-threads",
"synchronize",
"synckit"
],
"scripts": {
"build": "run-p build:*",
"build:r": "r -f esm,es2015",
"build:ts": "tsc -P src",
"lint": "run-p lint:*",
"lint:es": "eslint . --cache -f friendly --max-warnings 10",
"lint:tsc": "tsc --noEmit",
"postinstall": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prerelease": "yarn build",
"publish": "clean-publish",
"release": "changeset publish",
"test": "jest",
"typecov": "type-coverage"
},
"dependencies": {
"uuid": "^8.3.2"
},
"devDependencies": {
"@1stg/lib-config": "^1.2.10",
"@changesets/changelog-github": "^0.4.0",
"@changesets/cli": "^2.16.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.41",
"@types/uuid": "^8.3.0",
"clean-publish": "^2.1.1",
"npm-run-all": "^4.1.5",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
"type-coverage": "^2.17.3",
"typescript": "^4.2.4",
"yarn-deduplicate": "^3.1.0"
},
"commitlint": {
"extends": "@1stg"
},
"eslintConfig": {
"extends": "@1stg"
},
"eslintIgnore": [
"coverage",
"lib",
"CHANGELOG.md"
],
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"collectCoverage": true,
"moduleNameMapper": {
"^synckit$": "<rootDir>/src"
}
},
"prettier": "@1stg/prettier-config",
"remarkConfig": {
"plugins": [
"@1stg/remark-config"
]
},
"renovate": {
"extends": [
"@1stg"
]
},
"typeCoverage": {
"atLeast": 98.24,
"detail": true,
"strict": true,
"update": true
}
}
95 changes: 95 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { execSync } from 'child_process'
import { tmpdir as _tmpdir } from 'os'
import path from 'path'
import fs from 'fs'

import { v4 as uuid } from 'uuid'

/**
* @link https://github.com/sindresorhus/temp-dir/blob/main/index.js#L9
*/
export const tmpdir = fs.realpathSync(_tmpdir())

let tsconfigPathsAvailable: boolean

const isTsconfigPathsAvailable = () => {
if (typeof tsconfigPathsAvailable === 'boolean') {
return tsconfigPathsAvailable
}
try {
// eslint-disable-next-line node/no-extraneous-require
tsconfigPathsAvailable = !!require.resolve('tsconfig-paths')
} catch {
/**
* `require.resolve` can not be mocked to fail
* @link https://github.com/facebook/jest/issues/9543
*/
/* istanbul ignore next */
tsconfigPathsAvailable = false
}
return tsconfigPathsAvailable
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const syncFnCache = new Map<string, (...args: any) => any>()

export const createSyncFn = <T extends (...args: unknown[]) => unknown>(
workerPath: string,
) => {
if (!path.isAbsolute(workerPath)) {
throw new Error('`workerPath` must be absolute')
}

const cachedSyncFn = syncFnCache.get(workerPath)

if (cachedSyncFn) {
return cachedSyncFn
}

let resolvedWorkerPath = workerPath

if (!resolvedWorkerPath.endsWith('.ts')) {
resolvedWorkerPath = require.resolve(workerPath)
}

const executor = resolvedWorkerPath.endsWith('.ts')
? 'ts-node' +
(isTsconfigPathsAvailable()
? ' -r tsconfig-paths/register'
: /* istanbul ignore next */ '')
: 'node'

const syncFn = (...args: Parameters<T>): ReturnType<T> => {
const filename = path.resolve(tmpdir, `synckit-${uuid()}.json`)

fs.writeFileSync(filename, JSON.stringify(args))

const command = `${executor} ${resolvedWorkerPath} ${filename}`

try {
execSync(command, {
stdio: 'inherit',
})
const result = fs.readFileSync(filename, 'utf8')
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return JSON.parse(result)
} finally {
fs.unlinkSync(filename)
}
}

syncFnCache.set(workerPath, syncFn)

return syncFn
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const runAsWorker = async <R, T extends (...args: any[]) => Promise<R>>(
fn: T,
) => {
const filename = process.argv[2]
const content = fs.readFileSync(filename, 'utf-8')
const options = JSON.parse(content) as Parameters<T>
const result = await fn(...options)
fs.writeFileSync(filename, JSON.stringify(result))
}
7 changes: 7 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig",
"compilerOptions": {
"rootDir": ".",
"outDir": "../lib"
}
}

0 comments on commit e7446f9

Please sign in to comment.