@algolia/cli 4.0.7 → 5.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,72 +0,0 @@
1
- const fs = require('fs');
2
- const algolia = require('algoliasearch');
3
- const Base = require('./Base.js');
4
-
5
- class SetSettingsScript extends Base {
6
- constructor() {
7
- super();
8
- // Bind class methods
9
- this.getSource = this.getSource.bind(this);
10
- this.parseSetSettingsOptions = this.parseSetSettingsOptions.bind(this);
11
- this.start = this.start.bind(this);
12
- // Define validation constants
13
- this.message =
14
- '\nExample: $ algolia setsettings -a algoliaappid -k algoliaapikey -n algoliaindexname -s sourcefilepath -p setsettingsparams\n\n';
15
- this.params = [
16
- 'algoliaappid',
17
- 'algoliaapikey',
18
- 'algoliaindexname',
19
- 'sourcefilepath',
20
- ];
21
- }
22
-
23
- getSource(path) {
24
- const filepath = this.normalizePath(path);
25
- if (!fs.lstatSync(filepath).isFile())
26
- throw new Error('Source filepath must target valid settings file.');
27
- return filepath;
28
- }
29
-
30
- parseSetSettingsOptions(params) {
31
- try {
32
- const options = { forwardToReplicas: false };
33
- if (params === null) return options;
34
- else return JSON.parse(params);
35
- } catch (e) {
36
- throw e;
37
- }
38
- }
39
-
40
- async start(program) {
41
- try {
42
- // Validate command; if invalid display help text and exit
43
- this.validate(program, this.message, this.params);
44
-
45
- // Config params
46
- const appId = program.algoliaappid;
47
- const apiKey = program.algoliaapikey;
48
- const indexName = program.algoliaindexname;
49
- const sourceFilepath = program.sourcefilepath;
50
- const params = program.params || null;
51
-
52
- // Get index settings
53
- const settingsPath = this.getSource(sourceFilepath);
54
- const settingsFile = await fs.readFileSync(settingsPath);
55
- const settings = JSON.parse(settingsFile);
56
- // Get options
57
- const settingsOptions = this.parseSetSettingsOptions(params);
58
-
59
- // Instantiate Algolia index
60
- const client = algolia(appId, apiKey);
61
- const index = client.initIndex(indexName);
62
- // Set index settings
63
- const result = await index.setSettings(settings, settingsOptions);
64
- return console.log(result);
65
- } catch (e) {
66
- throw e;
67
- }
68
- }
69
- }
70
-
71
- const setSettingsScript = new SetSettingsScript();
72
- module.exports = setSettingsScript;
@@ -1,126 +0,0 @@
1
- const algolia = require('algoliasearch');
2
- const Base = require('./Base.js');
3
-
4
- class TransferIndexScript extends Base {
5
- constructor() {
6
- super();
7
- // Bind class methods
8
- this.getIndices = this.getIndices.bind(this);
9
- this.getTransformations = this.getTransformations.bind(this);
10
- this.transferIndexConfig = this.transferIndexConfig.bind(this);
11
- this.transferData = this.transferData.bind(this);
12
- this.start = this.start.bind(this);
13
- // Define validation constants
14
- this.message =
15
- '\nExample: $ algolia transferindex -a sourcealgoliaappid -k sourcealgoliaapikey -n sourcealgoliaindexname -d destinationalgoliaappid -y destinationalgoliaapikey -i destinationindexname -t transformationfilepath -e true\n\n';
16
- this.params = [
17
- 'sourcealgoliaappid',
18
- 'sourcealgoliaapikey',
19
- 'sourcealgoliaindexname',
20
- 'destinationalgoliaappid',
21
- 'destinationalgoliaapikey',
22
- ];
23
- }
24
-
25
- getIndices(options) {
26
- // Instantiate Algolia indices
27
- const sourceClient = algolia(options.sourceAppId, options.sourceApiKey);
28
- const sourceIndex = sourceClient.initIndex(options.sourceIndexName);
29
- const destinationClient = algolia(
30
- options.destinationAppId,
31
- options.destinationApiKey
32
- );
33
- const destinationIndex = destinationClient.initIndex(
34
- options.destinationIndexName
35
- );
36
-
37
- return { sourceIndex, destinationIndex };
38
- }
39
-
40
- getTransformations(options) {
41
- // Set JSON record transformations
42
- const transformations = options.transformations
43
- ? require(options.transformations)
44
- : null;
45
- // Validate transformations function input param
46
- const valid = transformations && typeof transformations === 'function';
47
- // Return provided transformation function if exists
48
- return valid ? transformations : null;
49
- }
50
-
51
- async transferIndexConfig(indices, options) {
52
- // Transfer settings, synonyms, and query rules
53
- const settings = await indices.sourceIndex.getSettings();
54
- const synonyms = await indices.sourceIndex.exportSynonyms();
55
- const rules = await indices.sourceIndex.exportRules();
56
- if (options.excludeReplicas) delete settings.replicas;
57
- await indices.destinationIndex.setSettings(settings);
58
- await indices.destinationIndex.batchSynonyms(synonyms);
59
- await indices.destinationIndex.batchRules(rules);
60
- }
61
-
62
- transferData(indices, formatRecord) {
63
- return new Promise((resolve, reject) => {
64
- // Export index
65
- const browse = indices.sourceIndex.browseAll('', {
66
- attributesToRetrieve: ['*'],
67
- });
68
- let hitsCount = 0;
69
- // Set browseAll event handlers
70
- browse.on('result', async result => {
71
- // Push hits to destination index
72
- try {
73
- const hits = formatRecord
74
- ? result.hits.map(formatRecord)
75
- : result.hits;
76
- await indices.destinationIndex.addObjects(hits);
77
- hitsCount += result.hits.length;
78
- this.writeProgress(`Records transferred: ${hitsCount}`);
79
- } catch (e) {
80
- throw e;
81
- }
82
- });
83
- browse.on('end', () => resolve('\nDone transferring index.\n'));
84
- browse.on('error', err => reject(err));
85
- });
86
- }
87
-
88
- async start(program) {
89
- try {
90
- // Validate command; if invalid display help text and exit
91
- this.validate(program, this.message, this.params);
92
-
93
- // Config params
94
- const options = {
95
- sourceAppId: program.sourcealgoliaappid,
96
- sourceApiKey: program.sourcealgoliaapikey,
97
- sourceIndexName: program.sourcealgoliaindexname,
98
- destinationAppId: program.destinationalgoliaappid,
99
- destinationApiKey: program.destinationalgoliaapikey,
100
- destinationIndexName:
101
- program.destinationindexname || program.sourcealgoliaindexname,
102
- transformations: program.transformationfilepath || null,
103
- excludeReplicas:
104
- program.excludereplicas !== undefined
105
- ? program.excludereplicas === 'true'
106
- : false,
107
- };
108
-
109
- // Configure Algolia clients/indices
110
- const indices = this.getIndices(options);
111
- // Configure transformations
112
- const formatRecord = this.getTransformations(options);
113
- // Transfer index configuration
114
- await this.transferIndexConfig(indices, options);
115
- // Transfer data
116
- const result = await this.transferData(indices, formatRecord);
117
-
118
- return console.log(result);
119
- } catch (e) {
120
- throw e;
121
- }
122
- }
123
- }
124
-
125
- const transferIndexScript = new TransferIndexScript();
126
- module.exports = transferIndexScript;
@@ -1,108 +0,0 @@
1
- const algolia = require('algoliasearch');
2
- const Base = require('./Base.js');
3
-
4
- class TransferIndexConfigScript extends Base {
5
- constructor() {
6
- super();
7
- // Bind class methods
8
- this.start = this.start.bind(this);
9
- this.getIndices = this.getIndices.bind(this);
10
- this.getConfigOptions = this.getConfigOptions.bind(this);
11
- this.transferIndexConfig = this.transferIndexConfig.bind(this);
12
- // Define validation constants
13
- this.message =
14
- '\nExample: $ algolia transferindexconfig -a sourcealgoliaappid -k sourcealgoliaapikey -n sourcealgoliaindexname -d destinationalgoliaappid -y destinationalgoliaapikey -i destinationindexname -p configParams -e true\n\n';
15
- this.params = [
16
- 'sourcealgoliaappid',
17
- 'sourcealgoliaapikey',
18
- 'sourcealgoliaindexname',
19
- 'destinationalgoliaappid',
20
- 'destinationalgoliaapikey',
21
- ];
22
- }
23
-
24
- getIndices(options) {
25
- // Instantiate Algolia indices
26
- const sourceClient = algolia(options.sourceAppId, options.sourceApiKey);
27
- const sourceIndex = sourceClient.initIndex(options.sourceIndexName);
28
- const destinationClient = algolia(
29
- options.destinationAppId,
30
- options.destinationApiKey
31
- );
32
- const destinationIndex = destinationClient.initIndex(
33
- options.destinationIndexName
34
- );
35
-
36
- return { sourceIndex, destinationIndex };
37
- }
38
-
39
- getConfigOptions(options) {
40
- // Default config
41
- const config = {
42
- sOptions: {},
43
- rOptions: {},
44
- };
45
- // No params provided, exit early
46
- if (!options.configParams) return config;
47
-
48
- const params = JSON.parse(options.configParams);
49
-
50
- // Set provided batchSynonyms and batchRules options
51
- if (params.batchSynonymsParams)
52
- config.sOptions = Object.assign({}, params.batchSynonymsParams);
53
- if (params.batchRulesParams)
54
- config.rOptions = Object.assign({}, params.batchRulesParams);
55
-
56
- return config;
57
- }
58
-
59
- async transferIndexConfig(indices, config, options) {
60
- // Transfer settings, synonyms, and query rules
61
- const settings = await indices.sourceIndex.getSettings();
62
- const synonyms = await indices.sourceIndex.exportSynonyms();
63
- const rules = await indices.sourceIndex.exportRules();
64
- if (options.excludeReplicas) delete settings.replicas;
65
- await indices.destinationIndex.setSettings(settings);
66
- await indices.destinationIndex.batchSynonyms(synonyms, config.sOptions);
67
- await indices.destinationIndex.batchRules(rules, config.rOptions);
68
- }
69
-
70
- async start(program) {
71
- try {
72
- // Validate command; if invalid display help text and exit
73
- this.validate(program, this.message, this.params);
74
-
75
- // Config params
76
- const options = {
77
- sourceAppId: program.sourcealgoliaappid,
78
- sourceApiKey: program.sourcealgoliaapikey,
79
- sourceIndexName: program.sourcealgoliaindexname,
80
- destinationAppId: program.destinationalgoliaappid,
81
- destinationApiKey: program.destinationalgoliaapikey,
82
- destinationIndexName:
83
- program.destinationindexname || program.sourcealgoliaindexname,
84
- configParams: program.params || null,
85
- excludeReplicas:
86
- program.excludereplicas !== undefined
87
- ? program.excludereplicas === 'true'
88
- : false,
89
- };
90
-
91
- // Configure Algolia clients/indices
92
- const indices = this.getIndices(options);
93
- // Configure batchSynonyms and batchRules options
94
- const config = this.getConfigOptions(options);
95
- // Transfer index configuration
96
- await this.transferIndexConfig(indices, config, options);
97
-
98
- return console.log(
99
- 'Index settings, synonyms, and query rules transferred successfully.'
100
- );
101
- } catch (e) {
102
- throw e;
103
- }
104
- }
105
- }
106
-
107
- const transferIndexConfigScript = new TransferIndexConfigScript();
108
- module.exports = transferIndexConfigScript;
@@ -1,136 +0,0 @@
1
- const fs = require('fs');
2
- const readLine = require('readline');
3
- const Base = require('./Base.js');
4
-
5
- class TransformLinesScript extends Base {
6
- constructor() {
7
- super();
8
- // Bind class methods
9
- this.defaultLineTransformation = this.defaultLineTransformation.bind(this);
10
- this.setOutput = this.setOutput.bind(this);
11
- this.setTransformations = this.setTransformations.bind(this);
12
- this.transformFile = this.transformFile.bind(this);
13
- this.init = this.init.bind(this);
14
- this.start = this.start.bind(this);
15
- // Define validation constants
16
- this.message =
17
- '\nExample: $ algolia transformlines -s sourcefilepath -o outputpath -t transformationfilepath \n\n';
18
- this.params = ['sourcefilepath'];
19
- }
20
-
21
- defaultLineTransformation(line) {
22
- // Default line transformation method
23
- /* eslint-disable no-control-regex */
24
- const newLine = line.match(/\u001e/, 'i')
25
- ? line.replace(/\u001e/, ',')
26
- : line;
27
- return newLine;
28
- /* eslint-enable no-control-regex */
29
- }
30
-
31
- setOutput(outputPath) {
32
- this.outputDir =
33
- outputPath !== null ? this.normalizePath(outputPath) : process.cwd();
34
-
35
- // Ensure outputpath is a directory
36
- if (!fs.lstatSync(this.outputDir).isDirectory())
37
- throw new Error('Output path must be a directory.');
38
- }
39
-
40
- setTransformations(transformationFilepath) {
41
- try {
42
- // Set JSON record transformations
43
- const transformations = transformationFilepath
44
- ? require(this.normalizePath(transformationFilepath))
45
- : null;
46
- // Validate transformations function input param
47
- const valid = transformations && typeof transformations === 'function';
48
- // Assign our transformations function using provided custom transformations file if exists
49
- this.lineTransformation = valid
50
- ? transformations
51
- : this.defaultLineTransformation;
52
- } catch (e) {
53
- throw e;
54
- }
55
- }
56
-
57
- // Method to transform an individual file line-by-line
58
- transformFile(filename) {
59
- return new Promise((resolve, reject) => {
60
- try {
61
- const writeStream = fs.createWriteStream(
62
- `${this.outputDir}/${filename}`
63
- );
64
- let count = 0;
65
-
66
- if (this.transformationFilepath === null) {
67
- writeStream.write('['); // Comment this out to prevent injecting opening bracket at start of new output file
68
- }
69
-
70
- const lineReader = readLine.createInterface({
71
- input: fs.createReadStream(`${this.directory}/${filename}`),
72
- });
73
-
74
- lineReader.on('line', line => {
75
- count++;
76
- const newLine = this.lineTransformation(line);
77
- this.writeProgress(`Line ${count}...`);
78
- writeStream.write(newLine);
79
- });
80
-
81
- lineReader.on('close', () => {
82
- console.log('Done writing!');
83
- if (this.transformationFilepath === null) {
84
- writeStream.write(']'); // Comment this out to prevent injecting closing bracket at end of new output file
85
- }
86
- writeStream.end();
87
- resolve(true);
88
- });
89
- } catch (e) {
90
- reject(e);
91
- }
92
- });
93
- }
94
-
95
- // Start script
96
- async init(filenames) {
97
- for (const filename of filenames) {
98
- try {
99
- console.log(`Reading: ${this.directory}/${filename}`);
100
- console.log(`Writing to: ${this.outputDir}/${filename}`);
101
- await this.transformFile(filename);
102
- } catch (e) {
103
- console.log(`Error while processing ${filename}`);
104
- throw new Error(e);
105
- }
106
- }
107
- }
108
-
109
- start(program) {
110
- // Script reads a file or directory of files synchronously, line-by-line.
111
- // Writes each file synchronously, line-by-line, to an output directory
112
- // while optionally applying a provided transformation function to each line.
113
-
114
- // Validate command; if invalid display help text and exit
115
- this.validate(program, this.message, this.params);
116
-
117
- // Config params
118
- this.sourceFilepath = program.sourcefilepath;
119
- this.outputpath = program.outputpath || null;
120
- this.transformationFilepath = program.transformationfilepath || null;
121
-
122
- // Configure source paths (this.directory, this.filenames)
123
- this.setSource({ sourceFilepath: this.sourceFilepath });
124
- // Configure output path (this.outputDir)
125
- this.setOutput(this.outputpath);
126
- // Configure transformations (this.lineTransformation)
127
- this.setTransformations(this.transformationFilepath);
128
-
129
- // Execute line transformations
130
- this.init(this.filenames);
131
- return false;
132
- }
133
- }
134
-
135
- const transformLinesScript = new TransformLinesScript();
136
- module.exports = transformLinesScript;
package/commands.js DELETED
@@ -1,16 +0,0 @@
1
- module.exports = {
2
- addrules: require('./commands/AddRules.js'),
3
- addsynonyms: require('./commands/AddSynonyms.js'),
4
- deleteindicespattern: require('./commands/DeleteIndicesPattern.js'),
5
- export: require('./commands/Export.js'),
6
- exportrules: require('./commands/ExportRules.js'),
7
- exportsynonyms: require('./commands/ExportSynonyms.js'),
8
- getsettings: require('./commands/GetSettings.js'),
9
- import: require('./commands/Import.js'),
10
- interactive: require('./commands/Interactive.js'),
11
- search: require('./commands/Search.js'),
12
- setsettings: require('./commands/SetSettings.js'),
13
- transferindex: require('./commands/TransferIndex.js'),
14
- transferindexconfig: require('./commands/TransferIndexConfig.js'),
15
- transformlines: require('./commands/TransformLines.js'),
16
- };