Skip to content

Commit

Permalink
Removed some async optimizations to get more clarity and stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaid committed Jan 31, 2019
1 parent b92457b commit eb4228b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 51 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.0",
"version": "2.1.0",
"private": true,
"author": "Jaid <[email protected]> (https://github.com/Jaid)",
"babel": {
Expand All @@ -15,13 +15,14 @@
},
"dependencies": {
"better-docs": "^1.1.2",
"command-join": "^2.0.0",
"debug": "^4.1.1",
"first-existing-path": "^1.0.1",
"get-exec-file": "^1.1.0",
"jsdoc": "^3.5.5",
"jsdoc-babel": "^0.5.0",
"jsdoc-export-default-interop": "^0.3.1",
"tmp-promise": "^1.0.5",
"get-exec-file": "^1.1.0",
"tsd-jsdoc": "^2.1.2"
},
"peerDependencies": {
Expand Down
54 changes: 28 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import path from "path"
import {execSync} from "child_process"

import fs from "fs-extra"
import tmpPromise from "tmp-promise"
import firstExistingPath from "first-existing-path"
import getExecFile from "get-exec-file"
import readPkgUp from "read-pkg-up"
import {isObject} from "lodash"
import commandJoin from "command-join"

const debug = require("debug")("jsdoc-tsd-webpack-plugin")

const webpackId = "JsdocTsdWebpackPlugin"

const getHtmlConfigPath = async (compilation, configBase, template, options, configDir) => {
const getHtmlConfigPath = (compilation, configBase, template, options, configDir) => {
const config = {
...configBase,
opts: {
Expand All @@ -26,7 +27,7 @@ const getHtmlConfigPath = async (compilation, configBase, template, options, con
return configPath
}

const getTsdConfigPath = async (compilation, configBase, template, options, configDir) => {
const getTsdConfigPath = (compilation, configBase, template, options, configDir) => {
const config = {
...configBase,
opts: {
Expand Down Expand Up @@ -169,32 +170,33 @@ export default class {
debug(`tsd output file should be named ${this.options.autoTsdOutputFile}`)
}

const [htmlConfigPath, tsdConfigPath] = await Promise.all([
getHtmlConfigPath(compilation, configBase, htmlModulePath, this.options, tempDir),
getTsdConfigPath(compilation, configBase, tsdModulePath, this.options, tempDir),
])

debug(`JSDoc HTML command: "${process.execPath}" "${jsdocPath}" --configure "${htmlConfigPath}"`)
debug(`JSDoc TSD command: "${process.execPath}" "${jsdocPath}" --configure "${tsdConfigPath}"`)

const jsdocJobs = [htmlConfigPath, tsdConfigPath].map(configPath => getExecFile(process.execPath, [jsdocPath, "--configure", configPath], {
timeout: 1000 * 120,
}))
const jsdocResults = await Promise.all(jsdocJobs)

for (const jsdocResult of jsdocResults) {
if (jsdocResult.error) {
throw new Error(`JSDoc failed with Error: ${error.message}`)
}
if (jsdocResult.stderr) {
throw new Error(`JSDoc failed with error output: ${jsdocResult.stderr}`)
}
const setups = [
{
name: "TSD",
modulePath: tsdModulePath,
configFactory: getTsdConfigPath,
},
{
name: "HTML",
configFactory: getHtmlConfigPath,
modulePath: htmlModulePath,
},
]

for (const {name, modulePath, configFactory} of setups) {
const configPath = configFactory(compilation, configBase, modulePath, this.options, tempDir)
const command = commandJoin([process.execPath, jsdocPath, "--configure", configPath])
debug(`JSDoc ${name} command: ${command}`)
const out = execSync(command, {
timeout: 1000 * 120,
cwd: compiler.context,
encoding: "utf8",
})
debug(`JSDoc ${name} output: ${out}`)
}

debug(`JSDoc HTML stdout: ${jsdocResults[0].stdout}`)
debug(`JSDoc TSD stdout: ${jsdocResults[1].stdout}`)

if (this.options.autoTsdOutputFile) {
debug(`Copying ${this.options.autoTsdOutputFile} to ${path.join(compiler.options.output.path, this.options.autoTsdOutputFile |> path.basename)}`)
const tsdContent = fs.readFileSync(this.options.autoTsdOutputFile)
compilation.assets[path.basename(this.options.autoTsdOutputFile)] = {
source: () => tsdContent,
Expand Down
49 changes: 28 additions & 21 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const runWebpack = async (name, extraConfig) => {
}))
fs.ensureDirSync(path.join(__dirname, name, "info"))
fs.writeJsonSync(path.join(__dirname, name, "info", "stats.json"), stats.toJson())
return stats
}

it("should run", () => runWebpack("basic", {
Expand All @@ -41,26 +42,32 @@ it("should run with publishimo-webpack-plugin", () => runWebpack("with-publishim
],
}))

it("should run with {babel: true}", () => runWebpack("with-babel", {
plugins: [
new CleanWebpackPlugin,
new JsdocTsdWebpackPlugin({
babel: {
presets: ["jaid"],
},
}),
new PublishimoWebpackPlugin,
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\//,
use: {
loader: "babel-loader",
options: {presets: ["jaid"]},
it("should run with {babel: true}", async () => {
await runWebpack("with-babel", {
plugins: [
new CleanWebpackPlugin,
new JsdocTsdWebpackPlugin({
babel: {
presets: ["jaid"],
},
},
}),
new PublishimoWebpackPlugin,
],
},
}))
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules\//,
use: {
loader: "babel-loader",
options: {presets: ["jaid"]},
},
},
],
},
})
const tsdContent = fs.readFileSync(path.join(__dirname, "with-babel", "dist", "main.d.ts"), "utf8")
expect(tsdContent).toMatch("declare")
const htmlContent = fs.readFileSync(path.join(__dirname, "with-babel", "dist-jsdoc", "with-babel", "1.0.0", "index.html"), "utf8")
expect(htmlContent).toMatch("hi (with babel)")
})
3 changes: 2 additions & 1 deletion test/with-babel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
* @return {string} The cleaned string
*/
export default string => {
return string |> #.trim()
return string
|> #.trim()
}

0 comments on commit eb4228b

Please sign in to comment.