diff --git a/README.md b/README.md index 868d52d..7d2932f 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ npm install ``` +🚧 **Note importante** : la variable d'environnement `$CORPUSES_ROOT` doit être définie. Elle correspond au chemin du répertoire dans lequel seront stockés les fichiers moissonnés. + ### Vérification du fonctionnement ### Commande d'exécution des tests unitaires : @@ -105,7 +107,7 @@ ``` { "halApi":"", // L'adresse url de l'api Hal - "max_retry":2 // Nombre des retries en cas d'échec lors de l'interrogation de l'api HAL + "maxRetry":2 // Nombre des retries en cas d'échec lors de l'interrogation de l'api HAL } ``` @@ -116,3 +118,4 @@ | FolderExistError | Harvest folder already exist. | | FetchError | API-HAL request error. | | NoResultFound | No result found to harvest. | +| MissingEnvironmentVariable | A required environment variable must be set | \ No newline at end of file diff --git a/config/config.js b/config/config.js index 0ff8737..6716709 100644 --- a/config/config.js +++ b/config/config.js @@ -1,4 +1,4 @@ module.exports = { halApi: 'https://api.archives-ouvertes.fr/search?', - max_retry: 2 + maxRetry: 2 }; diff --git a/index.js b/index.js index a3313e8..d3643d5 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const fetch = require('node-fetch'); const fs = require('graceful-fs'); +const path= require('path'); const xmlFormatter = require('xml-formatter'); const utils = require('./lib/utils'); @@ -23,12 +24,23 @@ const dateBegin = docObject.halHarvestModifiedDateFrom; const dateEnd = docObject.halHarvestModifiedDateTo; - harvestPath = utils.getHarvestPath([ + if (!process.env.CORPUSES_ROOT) { + const err = { + code: 'MissingEnvironmentVariable', + _errMsg: 'Environment variable $CORPUSES_ROOT must be set' + }; + docObject.error = err; + return cb(err); + } + + const folderName = [ docObject.corpusName, utils.getDateYYYYMMDD(new Date(dateBegin)), 'to', - utils.getDateYYYYMMDD(new Date(dateEnd))].filter(Boolean).join('-') - ); + utils.getDateYYYYMMDD(new Date(dateEnd)) + ].filter(Boolean).join('-'); + + harvestPath = path.join(process.env.CORPUSES_ROOT, 'hal', folderName); if (fs.existsSync(harvestPath)) { const err = { diff --git a/lib/utils.js b/lib/utils.js index 6f7e6de..eed2bd4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -72,10 +72,6 @@ return `${urlBase}/?${sp.toString()}`; }; -utils.getHarvestPath = function (path) { - return (process.env.CORPUSES_ROOT ? process.env.CORPUSES_ROOT + '/hal/' + path : 'hal/' + path); -}; - utils.getProxyAgent = function () { // Get proxy env vars const httpProxy = process.env.HTTP_PROXY ? process.env.HTTP_PROXY : process.env.http_proxy; diff --git a/test/data/.gitkeep b/test/data/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/data/.gitkeep diff --git a/test/run.js b/test/run.js index 57d5896..39db83e 100644 --- a/test/run.js +++ b/test/run.js @@ -3,7 +3,7 @@ const pkg = require('../package.json'); const fs = require('graceful-fs'); const business = require('../index.js'); -const utils = require('../lib/utils.js'); +const path = require('path'); const chai = require('chai'); const expect = chai.expect; @@ -18,10 +18,12 @@ }; } +process.env.CORPUSES_ROOT = path.join(__dirname, 'data'); + let harvestPath; describe(pkg.name + '/index.js', function () { before(function () { - harvestPath = utils.getHarvestPath('hal-20211121-to-20211121'); + harvestPath = path.join(process.env.CORPUSES_ROOT, 'hal', 'hal-20211121-to-20211121'); if (fs.existsSync(harvestPath)) { fs.rmdirSync(harvestPath, { recursive: true }); } @@ -46,7 +48,7 @@ const docObject = initDocObject(); docObject.halHarvestModifiedDateFrom = '2021-10-20T00:00:00.000Z'; docObject.halHarvestModifiedDateTo = '2021-10-21T00:00:00.000Z'; - const harvestPath = utils.getHarvestPath('hal-20211020-to-20211021'); + const harvestPath = path.join(process.env.CORPUSES_ROOT, 'hal', 'hal-20211020-to-20211021'); fs.mkdirSync(harvestPath, { recursive: true }); business.doTheJob(docObject, function (err) { // harvest folder already exist