1

What I want to do is to replace doczrc.js base value from abc to the name I get that from .git/config. So I do not need to manually change the base in the doczrc. (For those people who fork the repo, they have to change it themselves, and it will has trouble to push and PR to the parent repo.) So I have to write a script to change the name for building the page.

I want to read the repo owner from '.git/config'.

...
[remote "origin"]
    url = git@github.test.result/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
...

I have another file which is used to build github doc page. The configuration file export an object.

export default {
  ...
  base: `/pages/abc/test`
  ...
}

I have used readfilesync and but I am not allowed to use readfilesync. I then tried readfile, it is async, and I use promise. However, I cannot get the value from promise. From online tutorial, it is all console.log. But I need to get the exact value and return to other file to use.

I have tried the way below, change the export default to an object, and replace base. However, it still export abc instead of the value I want. Is there a way I can solve this problem?

// script.js
const remote = 'origin'
const file = '.git/config'
function readConfig() {
  return new Promise((resolve, reject) => {
    const regex = /:(\S+)\//gm
    try {
      fs.readFile(configFile, (err, data) => {
        if (err) {
          console.log(err.stack)
        }
        let result = regex.exec(data.toString())
        resolve(result[1])
      })
    }
    catch (err) {
      console.log(err)
      reject(err)
    }
  })
}

// readConfig could get the value I want. This value is what I want to send to script.js, which is to replace t
readConfig().then(x => console.log(x))
ghPages.publish('.gh-pages', { remote }, err => {
 if (err) {
   console.error(error .')
   console.error(err)
 } else {
   console.info('success')
 }
})

module.exports = {
  readConfig,
}
// script.js
// I tried to change script.js in this way instead of just call the function 
`/pages/{readConfig()}/test`

import { readConfig } from './doczrc'

const exportObj = {
  base: `/pages/abc/test`
}

readConfig().then(res => {
  exportObj.base = '/pages/' + res + '/test/'
})

export default exportObj

I want to export '/pages/' + (the value I read from config) + /test/

the command run

npm run docs:build && node script.js which doczrc.js is the config file for doc build. script.js is the script that I use to update value in the script.js base. and publish the github page.


change file name more meaningful. doczrc.js ( original is a.js) script.js ( original is b.js)

Or there is other way I can solve my problem.

Wenkai Fan
  • 93
  • 1
  • 10
  • 1
    You should export a promise for the value, not export an object and then change a property after an arbitrary amount of time. – Bergi May 25 '19 at 12:29
  • Btw, the `try`/`catch` is unnecessary, you rather should `reject()` if the `readFile` callback gets called with an `err` argument. Also, I'd recommend to not read and parse the file yourself but rather to call [the `git-config` command](https://git-scm.com/docs/git-config) and have it resolve the key you're looking for. – Bergi May 25 '19 at 12:32
  • @Bergi Thank you. Could you explain clearly? for `a.js`, it exports a function that return a promise object when it is called in `b.js`. Did you mean a.js not export an object? But `a.js` is a configuration file that `npm run docs:build` immediately. – Wenkai Fan May 25 '19 at 17:18
  • @Bergi Do you mean like this? ```javascript const p = new Promise((resolve, reject) => { var foo = { base: readConfig().then(value => { `/pages/${value}/test/` }), } resolve(foo) }) export default p ``` If just run node doczrc.js, it can compile successfully. But when I run npm command, it will not be able to compile successfully. `./client/node_modules/pp-vx/dist/components/icons/icons.css 3:0 Module parse failed: Unexpected token (3:0) You may need an appropriate loader to handle this file type. ` Appreciated if further help. – Wenkai Fan May 25 '19 at 19:00
  • No, avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! Rather, `const exportPromise = readConfig().then(value => ({base: `/pages/${value}/test/`})); export default exportPromise;`. Of course, you will need to adjust the script that imports `a.js` so that it can handle the exported promise. – Bergi May 25 '19 at 21:09
  • @Bergi Thank you! But the file import a.js(doczrc.js) is not controlled by me. it is npm run doc:build. which will build the git hub page itself. I cannot touch that file. I only can touch these two files and also if necessary to change the command to satisfy the requirements. I am afraid I did not explain clearly I add more details in the question. – Wenkai Fan May 26 '19 at 03:40
  • Sorry, your module names (`a.js`, `b.js`, `script.js`, `doczrc.js`) and which one imports which other are totally incomprehensible. Can you clear that up, please? – Bergi May 26 '19 at 14:25
  • If you absolutely need a synchronous export because the script that is calling you needs that, then you cannot use promises. Go for `readFileSync` instead. – Bergi May 26 '19 at 14:26

0 Answers0