@kintone/data-loader
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -6,2 +6,10 @@ # Change Log | ||
## [0.1.3](https://github.com/kintone/js-sdk/compare/@kintone/data-loader@0.1.2...@kintone/data-loader@0.1.3) (2021-02-17) | ||
**Note:** Version bump only for package @kintone/data-loader | ||
## [0.1.2](https://github.com/kintone/js-sdk/compare/@kintone/data-loader@0.1.1...@kintone/data-loader@0.1.2) (2021-02-09) | ||
@@ -8,0 +16,0 @@ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var api_1 = require("../api"); | ||
var rest_api_client_1 = require("@kintone/rest-api-client"); | ||
const api_1 = require("../api"); | ||
const rest_api_client_1 = require("@kintone/rest-api-client"); | ||
jest.mock("@kintone/rest-api-client"); | ||
describe("api", function () { | ||
var USERNAME = "username"; | ||
var PASSWORD = "password"; | ||
var BASE_URL = "https://localhost"; | ||
var API_TOKEN = "api_token"; | ||
var PFX_FILE_PATH = "./dummy.pfx"; | ||
var PFX_FILE_PASSWORD = "pfx_password"; | ||
it("should pass username and password to the apiClient correctly", function () { | ||
var apiClient = api_1.buildRestAPIClient({ | ||
describe("api", () => { | ||
const USERNAME = "username"; | ||
const PASSWORD = "password"; | ||
const BASE_URL = "https://localhost"; | ||
const API_TOKEN = "api_token"; | ||
const PFX_FILE_PATH = "./dummy.pfx"; | ||
const PFX_FILE_PASSWORD = "pfx_password"; | ||
it("should pass username and password to the apiClient correctly", () => { | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -28,5 +28,5 @@ username: USERNAME, | ||
}); | ||
it("should pass guestSpaceId to the apiClient correctly", function () { | ||
var GUEST_SPACE_ID = "1"; | ||
var apiClient = api_1.buildRestAPIClient({ | ||
it("should pass guestSpaceId to the apiClient correctly", () => { | ||
const GUEST_SPACE_ID = "1"; | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -47,4 +47,4 @@ username: USERNAME, | ||
}); | ||
it("should pass apiToken to the apiClient correctly", function () { | ||
var apiClient = api_1.buildRestAPIClient({ | ||
it("should pass apiToken to the apiClient correctly", () => { | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -59,4 +59,4 @@ apiToken: API_TOKEN, | ||
}); | ||
it("should prioritize username and password over apiToken", function () { | ||
var apiClient = api_1.buildRestAPIClient({ | ||
it("should prioritize username and password over apiToken", () => { | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -76,6 +76,6 @@ username: USERNAME, | ||
}); | ||
it("should pass basic auth params to the apiClient correctly", function () { | ||
var BASIC_AUTH_USERNAME = "basic_auth_username"; | ||
var BASIC_AUTH_PASSWORD = "basic_auth_password"; | ||
var apiClient = api_1.buildRestAPIClient({ | ||
it("should pass basic auth params to the apiClient correctly", () => { | ||
const BASIC_AUTH_USERNAME = "basic_auth_username"; | ||
const BASIC_AUTH_PASSWORD = "basic_auth_password"; | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -100,4 +100,4 @@ username: USERNAME, | ||
}); | ||
it("should pass information of client certificate to the apiClient correctly", function () { | ||
var apiClient = api_1.buildRestAPIClient({ | ||
it("should pass information of client certificate to the apiClient correctly", () => { | ||
const apiClient = api_1.buildRestAPIClient({ | ||
baseUrl: BASE_URL, | ||
@@ -104,0 +104,0 @@ username: USERNAME, |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -25,18 +6,14 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var child_process_1 = __importStar(require("child_process")); | ||
var util_1 = require("util"); | ||
var path_1 = __importDefault(require("path")); | ||
var projectRoot = path_1.default.resolve(__dirname, "../.."); | ||
var exec = util_1.promisify(child_process_1.default.exec); | ||
var packageJson = require(path_1.default.resolve(projectRoot, "package.json")); | ||
var mainFilePath = path_1.default.resolve(projectRoot, packageJson.bin["kintone-data-loader"]); | ||
var checkRejectArg = function (_a) { | ||
var arg = _a.arg, errorMessage = _a.errorMessage; | ||
return expect(exec("cross-env LC_ALL='en_US' node " + mainFilePath + " " + arg)).rejects.toThrow(errorMessage); | ||
const child_process_1 = __importDefault(require("child_process")); | ||
const util_1 = require("util"); | ||
const path_1 = __importDefault(require("path")); | ||
const projectRoot = path_1.default.resolve(__dirname, "../.."); | ||
const exec = util_1.promisify(child_process_1.default.exec); | ||
const packageJson = require(path_1.default.resolve(projectRoot, "package.json")); | ||
const mainFilePath = path_1.default.resolve(projectRoot, packageJson.bin["kintone-data-loader"]); | ||
const checkRejectArg = ({ arg, errorMessage, }) => { | ||
return expect(exec(`cross-env LC_ALL='en_US' node ${mainFilePath} ${arg}`)).rejects.toThrow(errorMessage); | ||
}; | ||
describe("main", function () { | ||
beforeAll(function () { | ||
child_process_1.execSync("npm run build"); | ||
}); | ||
it("should throw error when no commands are passed", function () { | ||
describe("main", () => { | ||
it("should throw error when no commands are passed", () => { | ||
return checkRejectArg({ | ||
@@ -47,3 +24,3 @@ arg: "", | ||
}); | ||
it("should throw error when an undefined command is passed", function () { | ||
it("should throw error when an undefined command is passed", () => { | ||
return checkRejectArg({ | ||
@@ -54,3 +31,3 @@ arg: "dummy", | ||
}); | ||
it("should throw error when an undefined argument is passed", function () { | ||
it("should throw error when an undefined argument is passed", () => { | ||
return checkRejectArg({ | ||
@@ -57,0 +34,0 @@ arg: "import --dummy", |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildRestAPIClient = void 0; | ||
var rest_api_client_1 = require("@kintone/rest-api-client"); | ||
var buildAuthParam = function (options) { | ||
var passwordAuthParam = { | ||
const rest_api_client_1 = require("@kintone/rest-api-client"); | ||
const buildAuthParam = (options) => { | ||
const passwordAuthParam = { | ||
username: options.username, | ||
@@ -27,3 +16,3 @@ password: options.password, | ||
}; | ||
var buildBasicAuthParam = function (options) { | ||
const buildBasicAuthParam = (options) => { | ||
return options.basicAuthUsername && options.basicAuthPassword | ||
@@ -38,3 +27,3 @@ ? { | ||
}; | ||
var buildClientCertAuth = function (options) { | ||
const buildClientCertAuth = (options) => { | ||
return options.pfxFilePath && options.pfxFilePassword | ||
@@ -49,6 +38,6 @@ ? { | ||
}; | ||
var buildRestAPIClient = function (options) { | ||
return new rest_api_client_1.KintoneRestAPIClient(__assign(__assign(__assign({ baseUrl: options.baseUrl, auth: buildAuthParam(options) }, buildBasicAuthParam(options)), buildClientCertAuth(options)), (options.guestSpaceId ? { guestSpaceId: options.guestSpaceId } : {}))); | ||
const buildRestAPIClient = (options) => { | ||
return new rest_api_client_1.KintoneRestAPIClient(Object.assign(Object.assign(Object.assign({ baseUrl: options.baseUrl, auth: buildAuthParam(options) }, buildBasicAuthParam(options)), buildClientCertAuth(options)), (options.guestSpaceId ? { guestSpaceId: options.guestSpaceId } : {}))); | ||
}; | ||
exports.buildRestAPIClient = buildRestAPIClient; | ||
//# sourceMappingURL=api.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.handler = exports.builder = exports.desc = exports.command = void 0; | ||
var export_1 = require("../controllers/export"); | ||
const export_1 = require("../controllers/export"); | ||
exports.command = "export"; | ||
exports.desc = "export the records of the specified app"; | ||
var builder = function (yargs) { | ||
return yargs | ||
.option("base-url", { | ||
describe: "Kintone Base Url", | ||
default: process.env.KINTONE_BASE_URL, | ||
}) | ||
.option("username", { | ||
alias: "u", | ||
describe: "Kintone Username", | ||
default: process.env.KINTONE_USERNAME, | ||
}) | ||
.option("password", { | ||
alias: "p", | ||
describe: "Kintone Password", | ||
default: process.env.KINTONE_PASSWORD, | ||
}) | ||
.option("api-token", { | ||
describe: "App's API token", | ||
default: process.env.KINTONE_API_TOKEN, | ||
}) | ||
.option("basic-auth-username", { | ||
describe: "Kintone Basic Auth Username", | ||
default: process.env.KINTONE_BASIC_AUTH_USERNAME, | ||
}) | ||
.option("basic-auth-password", { | ||
describe: "Kintone Basic Auth Password", | ||
default: process.env.KINTONE_BASIC_AUTH_PASSWORD, | ||
}) | ||
.option("app", { | ||
describe: "The ID of the app", | ||
}) | ||
.option("guest-space-id", { | ||
describe: "The ID of guest space", | ||
default: process.env.KINTONE_GUEST_SPACE_ID, | ||
}) | ||
.option("attachment-dir", { | ||
describe: "Attachment file directory", | ||
type: "string", | ||
}) | ||
.option("format", { | ||
describe: "Output format", | ||
default: "json", | ||
}) | ||
.option("query", { | ||
alias: "q", | ||
describe: "The query string", | ||
}) | ||
.option("pfx-file-path", { | ||
describe: "The path to client certificate file", | ||
}) | ||
.option("pfx-file-password", { | ||
describe: "The password of client certificate file", | ||
}); | ||
}; | ||
const builder = (yargs) => yargs | ||
.option("base-url", { | ||
describe: "Kintone Base Url", | ||
default: process.env.KINTONE_BASE_URL, | ||
}) | ||
.option("username", { | ||
alias: "u", | ||
describe: "Kintone Username", | ||
default: process.env.KINTONE_USERNAME, | ||
}) | ||
.option("password", { | ||
alias: "p", | ||
describe: "Kintone Password", | ||
default: process.env.KINTONE_PASSWORD, | ||
}) | ||
.option("api-token", { | ||
describe: "App's API token", | ||
default: process.env.KINTONE_API_TOKEN, | ||
}) | ||
.option("basic-auth-username", { | ||
describe: "Kintone Basic Auth Username", | ||
default: process.env.KINTONE_BASIC_AUTH_USERNAME, | ||
}) | ||
.option("basic-auth-password", { | ||
describe: "Kintone Basic Auth Password", | ||
default: process.env.KINTONE_BASIC_AUTH_PASSWORD, | ||
}) | ||
.option("app", { | ||
describe: "The ID of the app", | ||
}) | ||
.option("guest-space-id", { | ||
describe: "The ID of guest space", | ||
default: process.env.KINTONE_GUEST_SPACE_ID, | ||
}) | ||
.option("attachment-dir", { | ||
describe: "Attachment file directory", | ||
type: "string", | ||
}) | ||
.option("format", { | ||
describe: "Output format", | ||
default: "json", | ||
}) | ||
.option("query", { | ||
alias: "q", | ||
describe: "The query string", | ||
}) | ||
.option("pfx-file-path", { | ||
describe: "The path to client certificate file", | ||
}) | ||
.option("pfx-file-password", { | ||
describe: "The password of client certificate file", | ||
}); | ||
exports.builder = builder; | ||
var handler = function (argv) { return export_1.run(argv); }; | ||
const handler = (argv) => export_1.run(argv); | ||
exports.handler = handler; | ||
//# sourceMappingURL=export.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.handler = exports.builder = exports.desc = exports.command = void 0; | ||
var import_1 = require("../controllers/import"); | ||
const import_1 = require("../controllers/import"); | ||
exports.command = "import"; | ||
exports.desc = "import the records of the specified app"; | ||
var builder = function (yargs) { | ||
return yargs | ||
.option("base-url", { | ||
describe: "Kintone Base Url", | ||
default: process.env.KINTONE_BASE_URL, | ||
}) | ||
.option("username", { | ||
alias: "u", | ||
describe: "Kintone Username", | ||
default: process.env.KINTONE_USERNAME, | ||
}) | ||
.option("password", { | ||
alias: "p", | ||
describe: "Kintone Password", | ||
default: process.env.KINTONE_PASSWORD, | ||
}) | ||
.option("api-token", { | ||
describe: "App's API token", | ||
default: process.env.KINTONE_API_TOKEN, | ||
}) | ||
.option("basic-auth-username", { | ||
describe: "Kintone Basic Auth Username", | ||
default: process.env.KINTONE_BASIC_AUTH_USERNAME, | ||
}) | ||
.option("basic-auth-password", { | ||
describe: "Kintone Basic Auth Password", | ||
default: process.env.KINTONE_BASIC_AUTH_PASSWORD, | ||
}) | ||
.option("app", { | ||
describe: "The ID of the app", | ||
}) | ||
.option("guest-space-id", { | ||
describe: "The ID of guest space", | ||
default: process.env.KINTONE_GUEST_SPACE_ID, | ||
}) | ||
.option("file-path", { | ||
describe: "file path", | ||
}) | ||
.option("pfx-file-path", { | ||
describe: "The path to client certificate file", | ||
}) | ||
.option("pfx-file-password", { | ||
describe: "The password of client certificate file", | ||
}).argv; | ||
}; | ||
const builder = (yargs) => yargs | ||
.option("base-url", { | ||
describe: "Kintone Base Url", | ||
default: process.env.KINTONE_BASE_URL, | ||
}) | ||
.option("username", { | ||
alias: "u", | ||
describe: "Kintone Username", | ||
default: process.env.KINTONE_USERNAME, | ||
}) | ||
.option("password", { | ||
alias: "p", | ||
describe: "Kintone Password", | ||
default: process.env.KINTONE_PASSWORD, | ||
}) | ||
.option("api-token", { | ||
describe: "App's API token", | ||
default: process.env.KINTONE_API_TOKEN, | ||
}) | ||
.option("basic-auth-username", { | ||
describe: "Kintone Basic Auth Username", | ||
default: process.env.KINTONE_BASIC_AUTH_USERNAME, | ||
}) | ||
.option("basic-auth-password", { | ||
describe: "Kintone Basic Auth Password", | ||
default: process.env.KINTONE_BASIC_AUTH_PASSWORD, | ||
}) | ||
.option("app", { | ||
describe: "The ID of the app", | ||
}) | ||
.option("guest-space-id", { | ||
describe: "The ID of guest space", | ||
default: process.env.KINTONE_GUEST_SPACE_ID, | ||
}) | ||
.option("file-path", { | ||
describe: "file path", | ||
}) | ||
.option("pfx-file-path", { | ||
describe: "The path to client certificate file", | ||
}) | ||
.option("pfx-file-password", { | ||
describe: "The password of client certificate file", | ||
}).argv; | ||
exports.builder = builder; | ||
var handler = function (argv) { return import_1.run(argv); }; | ||
const handler = (argv) => import_1.run(argv); | ||
exports.handler = handler; | ||
//# sourceMappingURL=import.js.map |
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -43,10 +16,10 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var export_1 = require("../export"); | ||
var rest_api_client_1 = require("@kintone/rest-api-client"); | ||
var fs_1 = require("fs"); | ||
var os_1 = __importDefault(require("os")); | ||
var path_1 = __importDefault(require("path")); | ||
describe("export", function () { | ||
var apiClient; | ||
beforeEach(function () { | ||
const export_1 = require("../export"); | ||
const rest_api_client_1 = require("@kintone/rest-api-client"); | ||
const fs_1 = require("fs"); | ||
const os_1 = __importDefault(require("os")); | ||
const path_1 = __importDefault(require("path")); | ||
describe("export", () => { | ||
let apiClient; | ||
beforeEach(() => { | ||
apiClient = new rest_api_client_1.KintoneRestAPIClient({ | ||
@@ -57,120 +30,89 @@ baseUrl: "https://localhost/", | ||
}); | ||
it("should not be failed", function () { | ||
it("should not be failed", () => { | ||
apiClient.record.getAllRecords = jest.fn().mockResolvedValue([{}]); | ||
return expect(export_1.exportRecords(apiClient, { app: "1", attachmentDir: "" })).resolves.not.toThrow(); | ||
}); | ||
it("should pass parameters to the apiClient correctly", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var getAllRecordsMockFn, APP_ID, QUERY; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
getAllRecordsMockFn = jest.fn().mockResolvedValue([{}]); | ||
apiClient.record.getAllRecords = getAllRecordsMockFn; | ||
APP_ID = 1; | ||
QUERY = 'Customer like "foo"'; | ||
return [4 /*yield*/, export_1.exportRecords(apiClient, { | ||
app: APP_ID, | ||
attachmentDir: "", | ||
query: QUERY, | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
expect(getAllRecordsMockFn.mock.calls[0][0]).toStrictEqual({ | ||
app: APP_ID, | ||
condition: QUERY, | ||
}); | ||
return [2 /*return*/]; | ||
} | ||
it("should pass parameters to the apiClient correctly", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const getAllRecordsMockFn = jest.fn().mockResolvedValue([{}]); | ||
apiClient.record.getAllRecords = getAllRecordsMockFn; | ||
const APP_ID = 1; | ||
const QUERY = 'Customer like "foo"'; | ||
yield export_1.exportRecords(apiClient, { | ||
app: APP_ID, | ||
attachmentDir: "", | ||
query: QUERY, | ||
}); | ||
}); }); | ||
it("can get records", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var records, actual; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
records = [ | ||
{ | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
{ | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
]; | ||
apiClient.record.getAllRecords = jest.fn().mockResolvedValue(records); | ||
return [4 /*yield*/, export_1.exportRecords(apiClient, { | ||
app: "1", | ||
attachmentDir: "", | ||
})]; | ||
case 1: | ||
actual = _a.sent(); | ||
expect(actual).toStrictEqual(records); | ||
return [2 /*return*/]; | ||
} | ||
expect(getAllRecordsMockFn.mock.calls[0][0]).toStrictEqual({ | ||
app: APP_ID, | ||
condition: QUERY, | ||
}); | ||
}); }); | ||
it("can download files to a specified directory", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var recordWithAttachment, testFileData, records, tempDir, actual, downloadFile; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
recordWithAttachment = { | ||
$id: { | ||
value: "2", | ||
}, | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
attachment: { | ||
type: "FILE", | ||
value: [ | ||
{ | ||
contentType: "text/plain", | ||
fileKey: "test-file-key", | ||
name: "test.txt", | ||
}, | ||
], | ||
}, | ||
}; | ||
testFileData = "test data"; | ||
records = [ | ||
recordWithAttachment, | ||
{ | ||
$id: { | ||
value: "3", | ||
}, | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
]; | ||
return [4 /*yield*/, fs_1.promises.mkdtemp(path_1.default.join(os_1.default.tmpdir(), "kintone-data-loader-"))]; | ||
case 1: | ||
tempDir = _a.sent(); | ||
apiClient.record.getAllRecords = jest.fn().mockResolvedValue(records); | ||
apiClient.file.downloadFile = jest.fn().mockResolvedValue(testFileData); | ||
return [4 /*yield*/, export_1.exportRecords(apiClient, { | ||
app: "1", | ||
attachmentDir: tempDir, | ||
})]; | ||
case 2: | ||
actual = _a.sent(); | ||
expect(actual).toStrictEqual(records); | ||
return [4 /*yield*/, fs_1.promises.readFile(path_1.default.join(tempDir, recordWithAttachment.$id.value, recordWithAttachment.attachment.value[0].name))]; | ||
case 3: | ||
downloadFile = _a.sent(); | ||
expect(downloadFile.toString()).toBe(testFileData); | ||
return [2 /*return*/]; | ||
} | ||
})); | ||
it("can get records", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const records = [ | ||
{ | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
{ | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
]; | ||
apiClient.record.getAllRecords = jest.fn().mockResolvedValue(records); | ||
const actual = yield export_1.exportRecords(apiClient, { | ||
app: "1", | ||
attachmentDir: "", | ||
}); | ||
}); }); | ||
it("should throw error when API response is error", function () { | ||
var error = new Error("error for test"); | ||
expect(actual).toStrictEqual(records); | ||
})); | ||
it("can download files to a specified directory", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const recordWithAttachment = { | ||
$id: { | ||
value: "2", | ||
}, | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
attachment: { | ||
type: "FILE", | ||
value: [ | ||
{ | ||
contentType: "text/plain", | ||
fileKey: "test-file-key", | ||
name: "test.txt", | ||
}, | ||
], | ||
}, | ||
}; | ||
const testFileData = "test data"; | ||
const records = [ | ||
recordWithAttachment, | ||
{ | ||
$id: { | ||
value: "3", | ||
}, | ||
fieldCode: { | ||
type: "SINGLE_LINE_TEXT", | ||
value: "value1", | ||
}, | ||
}, | ||
]; | ||
const tempDir = yield fs_1.promises.mkdtemp(path_1.default.join(os_1.default.tmpdir(), "kintone-data-loader-")); | ||
apiClient.record.getAllRecords = jest.fn().mockResolvedValue(records); | ||
apiClient.file.downloadFile = jest.fn().mockResolvedValue(testFileData); | ||
const actual = yield export_1.exportRecords(apiClient, { | ||
app: "1", | ||
attachmentDir: tempDir, | ||
}); | ||
expect(actual).toStrictEqual(records); | ||
const downloadFile = yield fs_1.promises.readFile(path_1.default.join(tempDir, recordWithAttachment.$id.value, recordWithAttachment.attachment.value[0].name)); | ||
expect(downloadFile.toString()).toBe(testFileData); | ||
})); | ||
it("should throw error when API response is error", () => { | ||
const error = new Error("error for test"); | ||
apiClient.record.getAllRecords = jest.fn().mockRejectedValueOnce(error); | ||
@@ -177,0 +119,0 @@ return expect(export_1.exportRecords(apiClient, { app: "1", attachmentDir: "" })).rejects.toThrow(error); |
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -43,8 +16,8 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var import_1 = require("../import"); | ||
var rest_api_client_1 = require("@kintone/rest-api-client"); | ||
var path_1 = __importDefault(require("path")); | ||
describe("import", function () { | ||
var apiClient; | ||
beforeEach(function () { | ||
const import_1 = require("../import"); | ||
const rest_api_client_1 = require("@kintone/rest-api-client"); | ||
const path_1 = __importDefault(require("path")); | ||
describe("import", () => { | ||
let apiClient; | ||
beforeEach(() => { | ||
apiClient = new rest_api_client_1.KintoneRestAPIClient({ | ||
@@ -55,109 +28,65 @@ baseUrl: "https://localhost/", | ||
}); | ||
describe("json", function () { | ||
it("should not be failed", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var reporter, importRecords; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiClient.record.addRecords = jest.fn().mockResolvedValue([{}]); | ||
reporter = jest.fn(); | ||
importRecords = import_1.buildImporter({ apiClient: apiClient, reporter: reporter }); | ||
return [4 /*yield*/, importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.json"), | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
expect(reporter).toHaveBeenCalledWith("SUCCESS: records[0 - 0]"); | ||
return [2 /*return*/]; | ||
} | ||
describe("json", () => { | ||
it("should not be failed", () => __awaiter(void 0, void 0, void 0, function* () { | ||
apiClient.record.addRecords = jest.fn().mockResolvedValue([{}]); | ||
const reporter = jest.fn(); | ||
const importRecords = import_1.buildImporter({ apiClient, reporter }); | ||
yield importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.json"), | ||
}); | ||
}); }); | ||
it("should throw error when API response is error", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var error, reporter, importRecords, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
error = new Error(); | ||
apiClient.record.addRecords = jest.fn().mockRejectedValueOnce(error); | ||
reporter = jest.fn(); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
importRecords = import_1.buildImporter({ apiClient: apiClient, reporter: reporter }); | ||
return [4 /*yield*/, importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.json"), | ||
})]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
expect(e_1).toBe(error); | ||
return [3 /*break*/, 4]; | ||
case 4: | ||
expect(reporter).toHaveBeenCalledWith("FAILED: records[0 - 0]"); | ||
expect.assertions(2); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
it("should throw error when API response is error when records[100 - 2999] includes bad data", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var error, reporter, importRecords, e_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
error = new Error(); | ||
apiClient.record.addRecords = jest | ||
.fn() | ||
.mockImplementationOnce(function (app, records) { return Promise.resolve(); }) | ||
.mockImplementationOnce(function (app, records) { return Promise.reject(error); }); | ||
reporter = jest.fn(); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
importRecords = import_1.buildImporter({ apiClient: apiClient, reporter: reporter }); | ||
return [4 /*yield*/, importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test_3000.json"), | ||
})]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_2 = _a.sent(); | ||
expect(e_2).toBe(error); | ||
return [3 /*break*/, 4]; | ||
case 4: | ||
expect(reporter).toHaveBeenNthCalledWith(1, "SUCCESS: records[0 - 99]"); | ||
expect(reporter).toHaveBeenNthCalledWith(2, "FAILED: records[100 - 2999]"); | ||
expect.assertions(3); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
expect(reporter).toHaveBeenCalledWith("SUCCESS: records[0 - 0]"); | ||
})); | ||
it("should throw error when API response is error", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const error = new Error(); | ||
apiClient.record.addRecords = jest.fn().mockRejectedValueOnce(error); | ||
const reporter = jest.fn(); | ||
try { | ||
const importRecords = import_1.buildImporter({ apiClient, reporter }); | ||
yield importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.json"), | ||
}); | ||
} | ||
catch (e) { | ||
expect(e).toBe(error); | ||
} | ||
expect(reporter).toHaveBeenCalledWith("FAILED: records[0 - 0]"); | ||
expect.assertions(2); | ||
})); | ||
it("should throw error when API response is error when records[100 - 2999] includes bad data", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const error = new Error(); | ||
apiClient.record.addRecords = jest | ||
.fn() | ||
.mockImplementationOnce((app, records) => Promise.resolve()) | ||
.mockImplementationOnce((app, records) => Promise.reject(error)); | ||
const reporter = jest.fn(); | ||
try { | ||
const importRecords = import_1.buildImporter({ apiClient, reporter }); | ||
yield importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test_3000.json"), | ||
}); | ||
} | ||
catch (e) { | ||
expect(e).toBe(error); | ||
} | ||
expect(reporter).toHaveBeenNthCalledWith(1, "SUCCESS: records[0 - 99]"); | ||
expect(reporter).toHaveBeenNthCalledWith(2, "FAILED: records[100 - 2999]"); | ||
expect.assertions(3); | ||
})); | ||
}); | ||
describe("csv", function () { | ||
it("should not be failed", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var reporter, importRecords; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiClient.record.addRecords = jest.fn().mockResolvedValue([{}]); | ||
reporter = jest.fn(); | ||
importRecords = import_1.buildImporter({ apiClient: apiClient, reporter: reporter }); | ||
return [4 /*yield*/, importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.csv"), | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
expect(reporter).toHaveBeenCalledWith("SUCCESS: records[0 - 0]"); | ||
return [2 /*return*/]; | ||
} | ||
describe("csv", () => { | ||
it("should not be failed", () => __awaiter(void 0, void 0, void 0, function* () { | ||
apiClient.record.addRecords = jest.fn().mockResolvedValue([{}]); | ||
const reporter = jest.fn(); | ||
const importRecords = import_1.buildImporter({ apiClient, reporter }); | ||
yield importRecords({ | ||
app: "1", | ||
filePath: path_1.default.resolve(__dirname, "./fixtures/test.csv"), | ||
}); | ||
}); }); | ||
expect(reporter).toHaveBeenCalledWith("SUCCESS: records[0 - 0]"); | ||
})); | ||
}); | ||
}); | ||
//# sourceMappingURL=import.test.js.map |
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -44,85 +17,36 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.exportRecords = exports.run = void 0; | ||
var fs_1 = require("fs"); | ||
var path_1 = __importDefault(require("path")); | ||
var p_queue_1 = __importDefault(require("p-queue")); | ||
var api_1 = require("../api"); | ||
var printer_1 = require("../printer"); | ||
var run = function (argv) { return __awaiter(void 0, void 0, void 0, function () { | ||
var apiClient, records, printer; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiClient = api_1.buildRestAPIClient(argv); | ||
return [4 /*yield*/, exportRecords(apiClient, argv)]; | ||
case 1: | ||
records = _a.sent(); | ||
printer = printer_1.buildPrinter(argv.format); | ||
printer(records); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
const fs_1 = require("fs"); | ||
const path_1 = __importDefault(require("path")); | ||
const api_1 = require("../api"); | ||
const printer_1 = require("../printer"); | ||
const run = (argv) => __awaiter(void 0, void 0, void 0, function* () { | ||
const apiClient = api_1.buildRestAPIClient(argv); | ||
const records = yield exportRecords(apiClient, argv); | ||
const printer = printer_1.buildPrinter(argv.format); | ||
printer(records); | ||
}); | ||
exports.run = run; | ||
function exportRecords(apiClient, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var app, attachmentDir, query, records, fetchFiles, queue; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
app = options.app, attachmentDir = options.attachmentDir, query = options.query; | ||
return [4 /*yield*/, apiClient.record.getAllRecords({ | ||
app: app, | ||
condition: query, | ||
})]; | ||
case 1: | ||
records = _a.sent(); | ||
// TODO: filter fields | ||
// TODO: extract attachment fields first | ||
if (!attachmentDir) { | ||
return [2 /*return*/, records]; | ||
} | ||
fetchFiles = function (record) { return __awaiter(_this, void 0, void 0, function () { | ||
var fileInfos, _i, fileInfos_1, fileInfo; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
fileInfos = getFileInfos(record); | ||
_i = 0, fileInfos_1 = fileInfos; | ||
_a.label = 1; | ||
case 1: | ||
if (!(_i < fileInfos_1.length)) return [3 /*break*/, 4]; | ||
fileInfo = fileInfos_1[_i]; | ||
return [4 /*yield*/, downloadAttachments(apiClient, record, attachmentDir, fileInfo)]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
_i++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
queue = new p_queue_1.default({ concurrency: 5 }); | ||
return [4 /*yield*/, queue.addAll(records.map(function (record) { | ||
return function () { | ||
return fetchFiles(record); | ||
}; | ||
}))]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/, records]; | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { app, attachmentDir, query } = options; | ||
const records = yield apiClient.record.getAllRecords({ | ||
app, | ||
condition: query, | ||
}); | ||
// TODO: filter fields | ||
// TODO: extract attachment fields first | ||
if (attachmentDir) { | ||
yield downloadAttachments(apiClient, records, attachmentDir); | ||
} | ||
return records; | ||
}); | ||
} | ||
exports.exportRecords = exportRecords; | ||
var getFileInfos = function (record) { | ||
const getFileInfos = (record) => { | ||
// console.debug(`>>>record ${recordId}`); | ||
var fileInfos = []; | ||
Object.values(record).forEach(function (field) { | ||
const fileInfos = []; | ||
Object.values(record).forEach((field) => { | ||
if (field.type === "FILE") { | ||
// @ts-expect-error field.value should be FileInformation[] type. | ||
field.value.forEach(function (fileInfo) { | ||
field.value.forEach((fileInfo) => { | ||
fileInfos.push(fileInfo); | ||
@@ -134,23 +58,15 @@ }); | ||
}; | ||
var downloadAttachments = function (apiClient, record, attachmentDir, fileInfo) { return __awaiter(void 0, void 0, void 0, function () { | ||
var fileKey, name, file, recordId, dir; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
fileKey = fileInfo.fileKey, name = fileInfo.name; | ||
return [4 /*yield*/, apiClient.file.downloadFile({ fileKey: fileKey })]; | ||
case 1: | ||
file = _a.sent(); | ||
recordId = record.$id.value; | ||
dir = path_1.default.resolve(attachmentDir, recordId); | ||
return [4 /*yield*/, fs_1.promises.mkdir(dir, { recursive: true })]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, fs_1.promises.writeFile(path_1.default.resolve(dir, name), Buffer.from(file))]; | ||
case 3: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
const downloadAttachments = (apiClient, records, attachmentDir) => __awaiter(void 0, void 0, void 0, function* () { | ||
for (const record of records) { | ||
const fileInfos = getFileInfos(record); | ||
for (const fileInfo of fileInfos) { | ||
const { fileKey, name } = fileInfo; | ||
const file = yield apiClient.file.downloadFile({ fileKey }); | ||
const recordId = record.$id.value; | ||
const dir = path_1.default.resolve(attachmentDir, recordId); | ||
yield fs_1.promises.mkdir(dir, { recursive: true }); | ||
yield fs_1.promises.writeFile(path_1.default.resolve(dir, name), Buffer.from(file)); | ||
} | ||
}); | ||
}); }; | ||
} | ||
}); | ||
//# sourceMappingURL=export.js.map |
@@ -11,29 +11,2 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -44,28 +17,18 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.buildImporter = exports.run = void 0; | ||
var api_1 = require("../api"); | ||
var parser_1 = require("../parser"); | ||
var fs_1 = require("fs"); | ||
var path_1 = __importDefault(require("path")); | ||
var CHUNK_LENGTH = 100; | ||
var run = function (argv) { return __awaiter(void 0, void 0, void 0, function () { | ||
var apiClient, importRecords; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiClient = api_1.buildRestAPIClient(argv); | ||
importRecords = exports.buildImporter({ apiClient: apiClient, reporter: console.log }); | ||
return [4 /*yield*/, importRecords(argv).catch(function () { | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
})]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
const api_1 = require("../api"); | ||
const parser_1 = require("../parser"); | ||
const fs_1 = require("fs"); | ||
const path_1 = __importDefault(require("path")); | ||
const CHUNK_LENGTH = 100; | ||
const run = (argv) => __awaiter(void 0, void 0, void 0, function* () { | ||
const apiClient = api_1.buildRestAPIClient(argv); | ||
const importRecords = exports.buildImporter({ apiClient, reporter: console.log }); | ||
yield importRecords(argv).catch(() => { | ||
// eslint-disable-next-line no-process-exit | ||
process.exit(1); | ||
}); | ||
}); }; | ||
}); | ||
exports.run = run; | ||
var buildImporter = function (_a) { | ||
var apiClient = _a.apiClient, reporter = _a.reporter; | ||
var extractFileType = function (filepath) { | ||
const buildImporter = ({ apiClient, reporter, }) => { | ||
const extractFileType = (filepath) => { | ||
// TODO this cannot detect file type without extensions | ||
@@ -75,55 +38,30 @@ return path_1.default.extname(filepath).split(".").pop() || ""; | ||
function importRecords(options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var app, filePath, buf, type, records; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
app = options.app, filePath = options.filePath; | ||
return [4 /*yield*/, fs_1.promises.readFile(filePath)]; | ||
case 1: | ||
buf = _a.sent(); | ||
type = extractFileType(filePath); | ||
records = parser_1.parser(type, buf.toString()); | ||
return [4 /*yield*/, addAllRecordsChunk(app, records)]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { app, filePath } = options; | ||
const buf = yield fs_1.promises.readFile(filePath); | ||
const type = extractFileType(filePath); | ||
const records = parser_1.parser(type, buf.toString()); | ||
yield addAllRecordsChunk(app, records); | ||
}); | ||
} | ||
function addAllRecordsChunk(app, allRecords) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var chunkStartIndex, chunkNextIndex, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
chunkStartIndex = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(chunkStartIndex < allRecords.length)) return [3 /*break*/, 6]; | ||
chunkNextIndex = allRecords.length < chunkStartIndex + CHUNK_LENGTH | ||
? allRecords.length | ||
: chunkStartIndex + CHUNK_LENGTH; | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 4, , 5]); | ||
return [4 /*yield*/, apiClient.record.addRecords({ | ||
app: app, | ||
records: allRecords.slice(chunkStartIndex, chunkNextIndex), | ||
})]; | ||
case 3: | ||
_a.sent(); | ||
reporter("SUCCESS: records[" + chunkStartIndex + " - " + (chunkNextIndex - 1) + "]"); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
e_1 = _a.sent(); | ||
reporter("FAILED: records[" + chunkStartIndex + " - " + (allRecords.length - 1) + "]"); | ||
throw e_1; | ||
case 5: | ||
chunkStartIndex = chunkNextIndex; | ||
return [3 /*break*/, 1]; | ||
case 6: return [2 /*return*/]; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let chunkStartIndex = 0; | ||
while (chunkStartIndex < allRecords.length) { | ||
const chunkNextIndex = allRecords.length < chunkStartIndex + CHUNK_LENGTH | ||
? allRecords.length | ||
: chunkStartIndex + CHUNK_LENGTH; | ||
try { | ||
yield apiClient.record.addRecords({ | ||
app, | ||
records: allRecords.slice(chunkStartIndex, chunkNextIndex), | ||
}); | ||
reporter(`SUCCESS: records[${chunkStartIndex} - ${chunkNextIndex - 1}]`); | ||
} | ||
}); | ||
catch (e) { | ||
reporter(`FAILED: records[${chunkStartIndex} - ${allRecords.length - 1}]`); | ||
throw e; | ||
} | ||
chunkStartIndex = chunkNextIndex; | ||
} | ||
}); | ||
@@ -130,0 +68,0 @@ } |
@@ -6,5 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var yargs_1 = __importDefault(require("yargs")); | ||
const yargs_1 = __importDefault(require("yargs")); | ||
// eslint-disable-next-line no-unused-expressions | ||
yargs_1.default.commandDir("commands").demandCommand().strict().help().argv; | ||
//# sourceMappingURL=main.js.map |
@@ -7,13 +7,12 @@ "use strict"; | ||
exports.parser = void 0; | ||
var sync_1 = __importDefault(require("csv-parse/lib/sync")); | ||
var csvParser = function (input) { | ||
var records = sync_1.default(input, { | ||
const sync_1 = __importDefault(require("csv-parse/lib/sync")); | ||
const csvParser = (input) => { | ||
const records = sync_1.default(input, { | ||
columns: true, | ||
skip_empty_lines: true, | ||
}); | ||
return records.map(function (record) { | ||
var keys = Object.keys(record); | ||
var row = {}; | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
return records.map((record) => { | ||
const keys = Object.keys(record); | ||
const row = {}; | ||
for (const key of keys) { | ||
row[key] = { | ||
@@ -26,3 +25,3 @@ value: record[key], | ||
}; | ||
var parser = function (type, data) { | ||
const parser = (type, data) => { | ||
switch (type) { | ||
@@ -34,3 +33,3 @@ case "json": | ||
default: | ||
throw new Error("Unexpected file type: " + type + " is unacceptable."); | ||
throw new Error(`Unexpected file type: ${type} is unacceptable.`); | ||
} | ||
@@ -37,0 +36,0 @@ }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildPrinter = void 0; | ||
var printJSON = function (value) { | ||
const printJSON = (value) => { | ||
console.log(JSON.stringify(value, null, 2)); | ||
}; | ||
var buildPrinter = function (type) { | ||
if (type === void 0) { type = "json"; } | ||
const buildPrinter = (type = "json") => { | ||
switch (type) { | ||
@@ -14,3 +13,3 @@ case "json": { | ||
default: { | ||
throw new Error("Unknown format type. '" + type + "' is unknown as a format option."); | ||
throw new Error(`Unknown format type. '${type}' is unknown as a format option.`); | ||
} | ||
@@ -17,0 +16,0 @@ } |
{ | ||
"name": "@kintone/data-loader", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"publishConfig": { | ||
@@ -17,13 +17,11 @@ "access": "public" | ||
"scripts": { | ||
"build": "npm-run-all -l -s clean -p build:*", | ||
"prebuild": "yarn clean", | ||
"build": "tsc --build --force", | ||
"postbuild": "yarn pkg", | ||
"lint": "run-p -l lint:*", | ||
"prepublishOnly": "npm run build", | ||
"prerelease": "npm-run-all -p lint test -s", | ||
"test": "jest", | ||
"test:ci": "jest --runInBand", | ||
"build:compile": "run-p compile", | ||
"clean": "rimraf lib bin", | ||
"compile": "tsc", | ||
"pkg": "npm run build && pkg . --out-path=bin", | ||
"fix:lint": "npm run lint:eslint -- --fix", | ||
"pkg": "pkg . --out-path=bin", | ||
"fix:lint": "yarn lint:eslint -- --fix", | ||
"fix:md": "prettier --write README.md", | ||
@@ -33,5 +31,3 @@ "fix": "run-p fix:*", | ||
"lint:md": "prettier --check README.md", | ||
"lint:ts": "tsc --noEmit", | ||
"start": "npm run compile -- --watch", | ||
"run-ts": "ts-node src/main.ts" | ||
"start": "yarn build --watch" | ||
}, | ||
@@ -61,12 +57,10 @@ "repository": { | ||
"cross-env": "^7.0.2", | ||
"pkg": "^4.4.9", | ||
"ts-node": "^9.1.1" | ||
"pkg": "^4.4.9" | ||
}, | ||
"dependencies": { | ||
"@kintone/rest-api-client": "^1.9.0", | ||
"@kintone/rest-api-client": "^1.10.0", | ||
"csv-parse": "^4.15.1", | ||
"p-queue": "^6.6.2", | ||
"yargs": "^15.4.1" | ||
}, | ||
"gitHead": "53faabd35c63e403d002c72e8873f44be3fa3fb7" | ||
"gitHead": "63634bcf4dd8fd9eea9694a6a1b569553d4f32cf" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
3
3
57360
769
- Removedp-queue@^6.6.2
- Removedeventemitter3@4.0.7(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedp-queue@6.6.2(transitive)
- Removedp-timeout@3.2.0(transitive)