canvaslms-cli 1.3.3 → 1.4.1

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.
package/lib/config.js CHANGED
@@ -1,135 +1,103 @@
1
- /**
2
- * Configuration management for Canvas CLI
3
- */
4
-
5
- const fs = require('fs');
6
- const path = require('path');
7
- const os = require('os');
8
-
9
- // Configuration file path in user's home directory
10
- const CONFIG_FILE = path.join(os.homedir(), '.canvaslms-cli-config.json');
11
-
12
- /**
13
- * Get default configuration structure
14
- */
15
- function getDefaultConfig() {
16
- return {
17
- domain: '',
18
- token: '',
19
- createdAt: new Date().toISOString(),
20
- lastUpdated: new Date().toISOString()
21
- };
22
- }
23
-
24
- /**
25
- * Load configuration from file
26
- */
27
- function loadConfig() {
28
- try {
29
- // Load from config file
30
- if (fs.existsSync(CONFIG_FILE)) {
31
- const configData = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
32
- if (configData.domain && configData.token) {
33
- // Clean up domain - remove https:// and trailing slashes
34
- const domain = configData.domain.replace(/^https?:\/\//, '').replace(/\/$/, '');
35
- return { domain, token: configData.token };
36
- }
37
- }
38
-
39
- // No configuration found
40
- console.error('❌ No Canvas configuration found!');
41
- console.error('\nPlease run "canvas config setup" to configure your Canvas credentials.');
42
- process.exit(1); } catch (error) {
43
- console.error(`❌ Error loading configuration: ${error.message}`);
44
- process.exit(1);
45
- }
46
- }
47
-
48
- /**
49
- * Save configuration to file
50
- */
51
- function saveConfig(domain, token) {
52
- try {
53
- const config = {
54
- domain: domain.replace(/^https?:\/\//, '').replace(/\/$/, ''),
55
- token,
56
- createdAt: fs.existsSync(CONFIG_FILE) ?
57
- JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8')).createdAt :
58
- new Date().toISOString(),
59
- lastUpdated: new Date().toISOString()
60
- };
61
-
62
- fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
63
- console.log(`✅ Configuration saved to ${CONFIG_FILE}`);
64
- return true;
65
- } catch (error) {
66
- console.error(`❌ Error saving configuration: ${error.message}`);
67
- return false;
68
- }
69
- }
70
-
71
- /**
72
- * Get configuration file path
73
- */
74
- function getConfigPath() {
75
- return CONFIG_FILE;
76
- }
77
-
78
- /**
79
- * Check if configuration file exists
80
- */
81
- function configExists() {
82
- return fs.existsSync(CONFIG_FILE);
83
- }
84
-
85
- /**
86
- * Read current configuration (if exists)
87
- */
88
- function readConfig() {
89
- try {
90
- if (fs.existsSync(CONFIG_FILE)) {
91
- return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
92
- }
93
- return null;
94
- } catch (error) {
95
- console.error(`❌ Error reading configuration: ${error.message}`);
96
- return null;
97
- }
98
- }
99
-
100
- /**
101
- * Delete configuration file
102
- */
103
- function deleteConfig() {
104
- try {
105
- if (fs.existsSync(CONFIG_FILE)) {
106
- fs.unlinkSync(CONFIG_FILE);
107
- console.log('✅ Configuration file deleted successfully.');
108
- return true;
109
- } else {
110
- console.log('ℹ️ No configuration file found.');
111
- return false;
112
- }
113
- } catch (error) {
114
- console.error(`❌ Error deleting configuration: ${error.message}`);
115
- return false;
116
- }
117
- }
118
-
119
- /**
120
- * Get the Canvas instance configuration
121
- */
122
- function getInstanceConfig() {
123
- return loadConfig();
124
- }
125
-
126
- module.exports = {
127
- loadConfig,
128
- getInstanceConfig,
129
- saveConfig,
130
- getConfigPath,
131
- configExists,
132
- readConfig,
133
- deleteConfig,
134
- getDefaultConfig
135
- };
1
+ /**
2
+ * Configuration management for Canvas CLI
3
+ */
4
+
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import os from 'os';
8
+
9
+ // Configuration file path in user's home directory
10
+ const CONFIG_FILE = path.join(os.homedir(), '.canvaslms-cli-config.json');
11
+
12
+ // Get default configuration structure
13
+ export function getDefaultConfig() {
14
+ return {
15
+ domain: '',
16
+ token: '',
17
+ createdAt: new Date().toISOString(),
18
+ lastUpdated: new Date().toISOString()
19
+ };
20
+ }
21
+
22
+ // Load configuration from file
23
+ export function loadConfig() {
24
+ try {
25
+ // Load from config file
26
+ if (fs.existsSync(CONFIG_FILE)) {
27
+ const configData = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
28
+ if (configData.domain && configData.token) {
29
+ // Clean up domain - remove https:// and trailing slashes
30
+ const domain = configData.domain.replace(/^https?:\/\//, '').replace(/\/$/, '');
31
+ return { domain, token: configData.token };
32
+ }
33
+ }
34
+ // No configuration found
35
+ console.error('No Canvas configuration found!');
36
+ console.error('\nPlease run "canvas config setup" to configure your Canvas credentials.');
37
+ process.exit(1);
38
+ } catch (error) {
39
+ console.error(`Error loading configuration: ${error.message}`);
40
+ process.exit(1);
41
+ }
42
+ }
43
+
44
+ // Save configuration to file
45
+ export function saveConfig(domain, token) {
46
+ try {
47
+ const config = {
48
+ domain: domain.replace(/^https?:\/\//, '').replace(/\/$/, ''),
49
+ token,
50
+ createdAt: new Date().toISOString(),
51
+ lastUpdated: new Date().toISOString()
52
+ };
53
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8');
54
+ return true;
55
+ } catch (error) {
56
+ console.error(`Error saving configuration: ${error.message}`);
57
+ return false;
58
+ }
59
+ }
60
+
61
+ // Check if config file exists
62
+ export function configExists() {
63
+ return fs.existsSync(CONFIG_FILE);
64
+ }
65
+
66
+ // Get config file path
67
+ export function getConfigPath() {
68
+ return CONFIG_FILE;
69
+ }
70
+
71
+ // Delete config file
72
+ export function deleteConfig() {
73
+ try {
74
+ if (fs.existsSync(CONFIG_FILE)) {
75
+ fs.unlinkSync(CONFIG_FILE);
76
+ return true;
77
+ }
78
+ return false;
79
+ } catch (error) {
80
+ console.error(`Error deleting configuration: ${error.message}`);
81
+ return false;
82
+ }
83
+ }
84
+
85
+ // Read current configuration (if exists)
86
+ export function readConfig() {
87
+ try {
88
+ if (fs.existsSync(CONFIG_FILE)) {
89
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
90
+ }
91
+ return null;
92
+ } catch (error) {
93
+ console.error(`Error reading configuration: ${error.message}`);
94
+ return null;
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Get the Canvas instance configuration
100
+ */
101
+ export function getInstanceConfig() {
102
+ return loadConfig();
103
+ }
@@ -1,78 +1,73 @@
1
- /**
2
- * File upload utilities for Canvas
3
- */
4
-
5
- const fs = require('fs');
6
- const axios = require('axios');
7
- const FormData = require('form-data');
8
- const { makeCanvasRequest } = require('./api-client');
9
-
10
- /**
11
- * Upload single file to Canvas and return the file ID
12
- */
13
- async function uploadSingleFileToCanvas(courseId, assignmentId, filePath) {
14
- try {
15
- // Check if file exists
16
- if (!fs.existsSync(filePath)) {
17
- throw new Error(`File not found: ${filePath}`);
18
- }
19
-
20
- const fileName = require('path').basename(filePath);
21
- const fileContent = fs.readFileSync(filePath);
22
-
23
- // Step 1: Get upload URL from Canvas
24
- const uploadParams = [
25
- `name=${encodeURIComponent(fileName)}`,
26
- `size=${fileContent.length}`,
27
- 'parent_folder_path=/assignments'
28
- ];
29
-
30
- const uploadData = await makeCanvasRequest('post', `courses/${courseId}/assignments/${assignmentId}/submissions/self/files`, uploadParams);
31
-
32
- if (!uploadData.upload_url) {
33
- throw new Error('Failed to get upload URL from Canvas');
34
- }
35
-
36
- // Step 2: Upload file to the provided URL
37
- const form = new FormData();
38
-
39
- // Add all the required fields from Canvas response
40
- Object.keys(uploadData.upload_params).forEach(key => {
41
- form.append(key, uploadData.upload_params[key]);
42
- });
43
-
44
- // Add the file
45
- form.append('file', fileContent, fileName);
46
-
47
- const uploadResponse = await axios.post(uploadData.upload_url, form, {
48
- headers: form.getHeaders(),
49
- maxRedirects: 0,
50
- validateStatus: (status) => status < 400
51
- });
52
-
53
- // Return the file ID for later submission
54
- return uploadData.id || uploadResponse.data.id;
55
-
56
- } catch (error) {
57
- throw new Error(`Failed to upload file ${filePath}: ${error.message}`);
58
- }
59
- }
60
-
61
- /**
62
- * Submit assignment with uploaded files
63
- */
64
- async function submitAssignmentWithFiles(courseId, assignmentId, fileIds) {
65
- const submissionData = {
66
- submission: {
67
- submission_type: 'online_upload',
68
- file_ids: fileIds
69
- }
70
- };
71
-
72
- return await makeCanvasRequest('post', `courses/${courseId}/assignments/${assignmentId}/submissions`, [], JSON.stringify(submissionData));
73
- }
74
-
75
- module.exports = {
76
- uploadSingleFileToCanvas,
77
- submitAssignmentWithFiles
78
- };
1
+ /**
2
+ * File upload utilities for Canvas
3
+ */
4
+
5
+ import fs from 'fs';
6
+ import axios from 'axios';
7
+ import FormData from 'form-data';
8
+ import { makeCanvasRequest } from './api-client.js';
9
+
10
+ /**
11
+ * Upload single file to Canvas and return the file ID
12
+ */
13
+ export async function uploadSingleFileToCanvas(courseId, assignmentId, filePath) {
14
+ try {
15
+ // Check if file exists
16
+ if (!fs.existsSync(filePath)) {
17
+ throw new Error(`File not found: ${filePath}`);
18
+ }
19
+
20
+ const fileName = require('path').basename(filePath);
21
+ const fileContent = fs.readFileSync(filePath);
22
+
23
+ // Step 1: Get upload URL from Canvas
24
+ const uploadParams = [
25
+ `name=${encodeURIComponent(fileName)}`,
26
+ `size=${fileContent.length}`,
27
+ 'parent_folder_path=/assignments'
28
+ ];
29
+
30
+ const uploadData = await makeCanvasRequest('post', `courses/${courseId}/assignments/${assignmentId}/submissions/self/files`, uploadParams);
31
+
32
+ if (!uploadData.upload_url) {
33
+ throw new Error('Failed to get upload URL from Canvas');
34
+ }
35
+
36
+ // Step 2: Upload file to the provided URL
37
+ const form = new FormData();
38
+
39
+ // Add all the required fields from Canvas response
40
+ Object.keys(uploadData.upload_params).forEach(key => {
41
+ form.append(key, uploadData.upload_params[key]);
42
+ });
43
+
44
+ // Add the file
45
+ form.append('file', fileContent, fileName);
46
+
47
+ const uploadResponse = await axios.post(uploadData.upload_url, form, {
48
+ headers: form.getHeaders(),
49
+ maxRedirects: 0,
50
+ validateStatus: (status) => status < 400
51
+ });
52
+
53
+ // Return the file ID for later submission
54
+ return uploadData.id || uploadResponse.data.id;
55
+
56
+ } catch (error) {
57
+ throw new Error(`Failed to upload file ${filePath}: ${error.message}`);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Submit assignment with uploaded files
63
+ */
64
+ export async function submitAssignmentWithFiles(courseId, assignmentId, fileIds) {
65
+ const submissionData = {
66
+ submission: {
67
+ submission_type: 'online_upload',
68
+ file_ids: fileIds
69
+ }
70
+ };
71
+
72
+ return await makeCanvasRequest('post', `courses/${courseId}/assignments/${assignmentId}/submissions`, [], JSON.stringify(submissionData));
73
+ }